469,282 Members | 1,664 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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 16790
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by shapper | last post: by
3 posts views Thread by Leo Seccia | last post: by
11 posts views Thread by Tony | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.