'string' does not contain a definition for 'Contains'
Asked Answered
R

4

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?

Righthanded answered 10/9, 2012 at 0:3 Comment(0)
M
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.

Mitsukomitt answered 10/9, 2012 at 0:6 Comment(16)
My intellisense would argue with you. It tells me I have three overloads.Righthanded
@JeremyHolovacs: And one of them takes a StringComparer? (Sorry, I can't try it right now, but MSDN is usually right...)Mitsukomitt
as the error message implies, it takes an IEqualityComparer, which StringComparer implements.Righthanded
@JeremyHolovacs - are you sure you aren't using some kind of third party extension that supplies additional methods?Forever
@JeremyHolovacs extenstion methods?Michealmicheil
I think this is part of Linq.Righthanded
@JeremyHolovacs: Right... that's odd. Well, try IndexOf anyway.Mitsukomitt
My intellisense said this: (extention) bool IEnumerable<char>.Contains(char value, IEqualityComparer<char> comparer)Shied
@minitech Changing it to IndexOf gets me "The best overloaded method match for 'string.IndexOf(string, System.StringComparison)' has some invalid arguments"... I am scratching my head on this.Righthanded
Hah never mind I'm an idiot; IndexOf works if I change the StringComparer to StringComparison... Contains still doesn't work though. Well, it's good enough for me, thanks.Righthanded
@JeremyHolovacs: Is saup.UserPayment actually a string?Mitsukomitt
@JeremyHolovacs: Oh yeah, and I just realized the reason Contains doesn't work; String.Contains is a special case because a string is an IEnumerable<char>, not an IEnumerable<string>.Mitsukomitt
@minitech Can you expand your answer to explain? It might be useful for the next person scratching their head. I am marking this as the answer; thanks.Righthanded
@JeremyHolovacs: Sure, sorry.Mitsukomitt
Then what the hell is this sirs? msdn.microsoft.com/en-us/library/dy85x1sa.aspxBetsybetta
@Paul-SebastianManole: Documentation for a method called Contains on String that takes one argument, the second of which, it follows, is not an IEqualityComparer<string>.Mitsukomitt
A
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.

Above answered 10/9, 2012 at 0:19 Comment(1)
Odd that the intellisense lead me right into the problem. The IndexOf worked for what I needed, but it seems like that should work.Righthanded
T
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.

Tooth answered 10/9, 2012 at 0:21 Comment(0)
W
-4

Add following namespace to your class,

using System.Linq;

Hope it helps.

Wysocki answered 13/3, 2016 at 1:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.