Alex Rivera | Logout

Linq contains confusion

Asked 2010-02-02T16:48:32.590
10

I have noticed something odd with linq and the Contains method. It seems to get confused on which Contains method to call.

if (myString.Contains(strVar, StringComparison.OrdinalIgnoreCase))
{
  // Code here                  
}

The above code doesn't compile with the following error:

The type arguments for method 'System.Linq.Enumerable.Contains(System.Collections.Generic.IEnumerable, TSource, System.Collections.Generic.IEqualityComparer)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

If I remove the using linq statement it is happy with the contains (but brakes all the linq code).

What is the correct syntax to tell the compiler I want to use the String.Contains method and not Linqs?

Cheers

Edit
Report

1 Answer

3

What is the correct syntax to tell the compiler I want to use the String.Contains method and not Linqs?

There is no overload of String.Contains that accepts a StringComparision. You might want to use String.IndexOf(string, StringComparison):

// s is string
if(s.IndexOf(strVar, StringComparison.OrdinalIgnoreCase) >= 0) {
    // code here
}
answered 2010-02-02T16:54:05.137

Your Answer