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

Jeremy Holovacs picture Jeremy Holovacs · Sep 10, 2012 · Viewed 15.8k times · Source

I have a statement like so:

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 seems to be exactly what I'm supposed to do, but I get the whole line with the Contains method underlined with the following message:

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

What am I doing wrong?

Answer

Ry- picture Ry- · Sep 10, 2012

Try IndexOf:

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

The reason it doesn't work is because the Contains extension method that accepts an IEqualityComparer<TSource> is operating on a String, which implements IEnumerable<char>, not IEnumerable<string>, so a string and an IEqualityComparer<string> can't be passed to it.