Compare Strings in Linq. Need advice ...

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

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
 
shapper said:
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
 
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
 
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
 
Why do you want to use CompareTo? It sounds like you are doing an
equality test, which is what you already have...?

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Table Format to Hold Data 16
Linq. String 5
Linq > Group 2
Lower 6
Linq. Select 2
SQL to LINQ 1
Linq. Aggregate 4
Compare and Null 4

Back
Top