'string' does not contain a definition for 'Contains'

I have a statement like:

var vals = from StandAloneUserPayment saup in _Session.Query<StandAloneUserPayment>() .Fetch(x => x.RecurringPayments) where saup.User.UserId == userId && searchString.Contains(saup.FriendlyName, StringComparer.InvariantCultureIgnoreCase) select saup; 

This is similar to what I should do, but I get the entire string using the Contains method, underlined by the following message:

string does not contain a definition for Contains , and the best method of overloading System.Linq.ParallelEnumerable.Contains<TSource>(System.Linq.ParallelQuery<TSource>, TSource, System.Collections.Generic.IEqualityComparer<TSource>) has some invalid arguments

What am I doing wrong?

+6
source share
4 answers

Try IndexOf :

 searchString.IndexOf(saup.FriendlyName, StringComparison.InvariantCultureIgnoreCase) != -1 

The reason this doesn't work is because the Contains extension method, which accepts IEqualityComparer<TSource> , works on a String that implements IEnumerable<char> , not IEnumerable<string> , so a String and IEqualityComparer<string> cannot be transferred to him.

+13
source

Even if Contains exists (source, element, comparer) , you cannot * use this with NHibernate, because the code, not the expression trees that NH can translate.

*: that is, if you did not write a LINQ provider extension that generates special cases for your comparator, but it is not.

+2
source

. The components you use come from LINQ - it sees the line as IEnumerable<char> , so it wants IEqualityComparer<char> . StringComparer components implement IEqualityComparer<String> .

If the minitech IndexOf method works, I think this will be the easiest way.

+1
source

Add the following namespace to your class,

 using System.Linq; 

Hope this helps.

-2
source

Source: https://habr.com/ru/post/924981/


All Articles