473,785 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compare Strings in Linq. Need advice ...

Hello,

I want to compare two strings in a Linq Query.

In this case "Car", "cAr", "CAR" would all be the same.

Should I use ==, equals, ... ?

What is the best way to do this?

Thanks,
Miguel
Aug 22 '08 #1
6 17597
shapper wrote:
I want to compare two strings in a Linq Query.

In this case "Car", "cAr", "CAR" would all be the same.

Should I use ==, equals, ... ?

What is the best way to do this?
One of the String.Compare' s.

Arne

Aug 22 '08 #2
On Thu, 21 Aug 2008 18:47:19 -0700, shapper <md*****@gmail. comwrote:
Hello,

I want to compare two strings in a Linq Query.

In this case "Car", "cAr", "CAR" would all be the same.

Should I use ==, equals, ... ?
For the String class, == or Equals() should be equivalent. Of course,
you'll have to convert the strings to all lower or all upper case or
(preferably, IMHO) use one of the String.Compare( ) overloads that allows
you to tell it to ignore case (in that case, you'd use neither == or the
Equals() method). Take care to choose your culture-dependent or
culture-invariant case-handling as appropriate to your needs.

Pete
Aug 22 '08 #3
What is the best way to do this?

It depends *which* LINQ back-end. If you mean LINQ-to-SQL against a
case-insensitive database, it won't matter. If you mean LINQ-to-
Objects, then one of the overloads that accepts the comparison type
(such as case-insensitive) would be preferable. However, for LINQ-to-
SQL against a case-sensitive database, I think .ToLower / .ToUpper
might be your necessity, an you'll have to just avoid the Turkish i
problem. For index use, you might want to store a case-normalized
version of the value so you can hit that...

Marc
Aug 22 '08 #4
On Aug 22, 5:03*am, Marc Gravell <marc.grav...@g mail.comwrote:
What is the best way to do this?

It depends *which* LINQ back-end. If you mean LINQ-to-SQL against a
case-insensitive database, it won't matter. If you mean LINQ-to-
Objects, then one of the overloads that accepts the comparison type
(such as case-insensitive) would be preferable. However, for LINQ-to-
SQL against a case-sensitive database, I think .ToLower / .ToUpper
might be your necessity, an you'll have to just avoid the Turkish i
problem. For index use, you might want to store a case-normalized
version of the value so you can hit that...

Marc

I am using SQL Server 2005 so I think culture does not matter here ...
in fact if I use it I get an error.

I am using the following:

bool check = (from t in database.Tags
where t.Name == Name
select t).Any();

How would I use String.Compare in this? I tried but I get an error:
Cannot implicitly convert type 'int' to 'bool'

bool check = (from t in database.Tags
where t.Name.CompareT o(Name)
select t).Any();

Basically I am only trying the best way to check if a tag with given
Name exists in database Tags.

Thanks,
Miguel
Aug 22 '08 #5
Why do you want to use CompareTo? It sounds like you are doing an
equality test, which is what you already have...?

Marc
Aug 22 '08 #6
On Fri, 22 Aug 2008 04:16:09 -0700, shapper <md*****@gmail. comwrote:
[...]
How would I use String.Compare in this? I tried but I get an error:
Cannot implicitly convert type 'int' to 'bool'
That's because the CompareTo() method returns an int, not a bool.

If you're using the CompareTo() method, then I agree with Marc that you
should just use the == operator.

But your original question indicated you wanted a case-insensitive
comparison, in which case you'd use one of the String.Compare( ) overloads,
not String.CompareT o().

Resolving both mistakes at once, you could have:

bool check = (from t in database.Tags
where String.Compare( t.Name, Name,
StringCompariso n.InvariantCult ureIgnoreCase)
== 0
select t).Any();

Note the comparison of the result of Compare() to the value 0, which is
what it returns when the two strings are equal.

This also assumes, of course, that you want the invariant culture. There
are other comparison options that ignore case for other types of
comparisons.

Pete
Aug 22 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
37859
by: Jason | last post by:
Is there a mechanism in VB.NET that allows something like: If myVar In ("A","B","C") Then... The way I'm doing it now is: Select Case myVar Case "A","B","C" Or like this:
9
11373
by: Martoon | last post by:
I want to instantiate an STL map with my own compare function, and I want to pass a parameter to the compare function that will be stored and used for all comparisons in that map instance. As an example, let's say the map key is char* strings, and I want the comparison based on the nth character in the string. When the map is instantiated, I need to somehow specify n. So I might have something like this: class MyClass
1
1262
by: shapper | last post by:
Hello, I have been reading a few articles about LINQ and I have a few questions: 1. What do I need to start using it in my ASP.NET 2.0 / SQL 2005 / Visual Studio 2005 web sites? 2. Is there a way to convert my existing SQL Stored Procedures to LINQ code?
3
1442
by: shapper | last post by:
Hello, I need an advice: I have 3 tables: Posts, Events and Files. Each post, event and file can be a associated to one or many tags. My idea was to create only one Tags table. Note that each tag can have various associations. It can be associate to various posts, events and files simultaneous.
3
2903
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table: tlkpColor (PK) tlkpColorID
2
4135
by: Mike N. | last post by:
hello all: I am using LINQ to XML, but I think this issue is more widespread, here's an example of what I am trying to do: Dim testurl As String = "http://stats.enemyterritory.com/profile/ Hellfire.?xml=true" Dim pageContents As XElement = XElement.Load(testurl) The url in question is for a game statistics site where you can load
11
2340
by: Tony | last post by:
Hello! Below I have two different ways to test if the instance tb.Text contains the string "Programmer" So which one to use is it just a matter of taste ? Or could it be some advantage to one before the other. In a book I read they had used the second one but I prefer the first one because it it clearer.
3
10559
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the format was different. Basically you can sort a class having members LastName (string), FirstName (string) and Age (int) according to whether you want to sort by Last Name, First Name or Age, using the .Sort function
6
4344
by: aznimah | last post by:
hi, i'm work on image comparison. i'm using the similarity measurement which i need to: 1) convert the image into the binary form since the algorithm that i've use works with binary data for the computation 2) compare the string binary data to get the similarity or dissimilarity result. The problem is, i already done with the image (jpg) conversion to binary and also try the algorithm structure in C# language, but i having a problem to...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.