9

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?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254

4 Answers4

17

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.

Ry-
  • 218,210
  • 55
  • 464
  • 476
3

Even if there is a Contains(source, item, comparer) method, you can't* use that with NHibernate, as comparers are code, not expression trees that NH can translate.

*: that is, unless you write a LINQ provider extension that special-cases generation for your comparer, but that's not the case.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • Odd that the intellisense lead me right into the problem. The IndexOf worked for what I needed, but it seems like that should work. – Jeremy Holovacs Sep 10 '12 at 00:21
2

The .Contains you are using comes from LINQ - it sees the String as an IEnumerable<char>, so it wants an IEqualityComparer<char>. The StringComparer comparers implement IEqualityComparer<String>.

If minitech's IndexOf method works, I think that will be the easiest way.

TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47
-3

Add following namespace to your class,

using System.Linq;

Hope it helps.

Jogi
  • 1
  • 2
  • 14
  • 29