Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq contains confusion

Tags:

c#

linq

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

like image 439
Magpie Avatar asked Feb 02 '10 16:02

Magpie


1 Answers

This is because there's no String.Contains(string, StringComparison) method defined in the BCL and the compiler tries to use an extension method. There's only String.Contains(string) method defined.

like image 79
Darin Dimitrov Avatar answered Oct 11 '22 12:10

Darin Dimitrov