Reputation: 12745
I am trying to do a string compassion as below:
var nDetails = listOfServiceUrls.Where(x => String.Equals(x.Description,
serviceName,StringComparison.InvariantCultureIgnoreCase));
But getting exception,
System.ArgumentException: Incorrect number of arguments supplied for call to method 'Boolean Equals(System.String, System.String, System.StringComparison)'
If I remove the where clause it works just fine!!
var nDetails = listOfServiceUrls.Where(x => String.Equals(x.Description,serviceName));
Upvotes: 1
Views: 2079
Reputation: 9587
You must be using Entity Framework or similar technology which actually needs to interpret the expression supplied as an argument to the Where
method. It will only have translations a small subset of Base Class Library (standard .NET lib) methods and their overloads.
Hover your cursor over your listOfServiceUrls
variable in the code editor and see what type it is. My money is on the fact that it's IQueryable<string>
(as opposed to IEnumerable<string>
).
EF does appear to have a translation for the instance Equals(string, StringComparison)
method on System.String
, so you can rewrite your code as follows:
var nDetails = listOfServiceUrls.Where(
x => x.Description.Equals(serviceName, StringComparison.InvariantCultureIgnoreCase));
Unlike .NET you will not see a NullReferenceException
if any of the strings in the collection happens to be NULL
- because the predicate will be executed by SQL Server as opposed to the .NET runtime.
Upvotes: 5
Reputation: 11763
According to Linqpad, it works flawlessly ... :
List<string> listOfServiceUrls = new List<string>{"one","two","three"};
var nDetails = listOfServiceUrls.Where(x => String.Equals(x,
"Two",StringComparison.InvariantCultureIgnoreCase));
nDetails.Dump();
Returns: two
.
Removing the ignore case will return nothing (since obviously, Two != two
.
Upvotes: 0