IEnumerable<type> does not contain a definition of 'Contains'

When you use Contains, the object you're looking for must match the type T of the IEnumerable<T>. Thus, you cannot search IEnumerable<A> for a contained object of type B since there's no implicit way to compare the two.

As mentioned in other answers, use Any and pass in the comparison yourself.

Alternatively, this is also a case where you could use a Select followed by Contains, although this may be less readable in some cases:

var query = values
    .Where(x => !holidays
        .Select(h => h.holiday)
        .Contains(x.someDate));

As an alternative to what everyone else has suggested already:

var holidayDates = new HashSet<DateTime>(holidays.Select(h => h.holiday));
var query = values.Where(x => !holidayDates.Contains(x.someDate));

In particular, if you have a lot of holidays, this change will make the per-value check much more efficient.


Contains is a LINQ extension that takes (in your case) an instance of Holidays and checks if your enumeration contains that instance (or an instance that Equals the given argument).

You should use Any instead:

var query = values.Where(x=> !holidays.Any(h => h.holiday == x.someDate));

Tags:

C#

.Net

Linq