473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 17563
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...@gmail.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.CompareTo(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.CompareTo().

Resolving both mistakes at once, you could have:

bool check = (from t in database.Tags
where String.Compare(t.Name, Name,
StringComparison.InvariantCultureIgnoreCase)
== 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
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
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...
1
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...
3
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...
3
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:...
2
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/...
11
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...
3
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...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.