LINQ Any () argument against parameter data type

I have an object with a string typed parameter called baan_cat_fam_code . The code below is my attempt to find all the elements in a query with baan_cat_fam_code that exist in a common list of strings called catFamCd .

query = query.Where(r => r.baan_cat_family_code.Any(s => catFamCode.Contains(s))); 

The problem is that this will not compile - I get an error message that indicates

 "Argument type 'char' is not assignable to parameter type 'string'" 

for some reason, the predicate s is printed as char. Therefore, I am adding .ToString () to the argument of the .Contains method. However, when the code works, I get the following exception that occurred when the query result is bound to a list.

 "The argument 'value' was the wrong type. Expected 'System.Char'. Actual 'System.String'." 

It made me scratch my head. Any help would be greatly appreciated.

Thanks!

+7
source share
2 answers

The problem you are facing is that baan_cat_family_code is of type string , which implements IEnumerable<char> . When you call Any , essentially

  • Is the predicate true for any char in this string

What you really want to ask is

  • Is this a string in the catFamCode list

Try the following that last

 query = query.Where(r => catFamCode.Contains(r.baan_cat_family_code)); 
+13
source

You call Any() on a line that looks at its elements, which are chars , therefore an error.

You should have done:

 query = query.Where(r => catFamCode.Contains(r.baan_cat_family_code)); 
+2
source

All Articles