class first
{
private int? firstID;
}
class second
{
private int secondID;
private int secondField;
}
public override Expression<Func<first, bool>> FirstFilter()
{
Contex db = new Contex();
List<second> list = (from p in db.second select p).ToList();
return b => list.Select(p => p.secondID).Contains(b.firstID);
}
and I have error:
cannot convert from ‘System.Collections.Generic.IEnumerable’ to ‘System.Collections.Generic.IEnumerable’
I have tried many different ways, but I just don’t know how can I fix it.
Use this:
You are getting the problem, because
list.Select(p => p.secondID)will be anIEnumerable<int?, but because firstID isint(non-nullable), overload resolution cannot determine a valid overload of Contains to call. You cannot implicitly convert fromIEnumerable<int?>toIEnumerable<int>. The Cast extension method works by casting each element to int.As mentioned in another answer, you could also simply pass in a non-nullable int into Contains:
But, do be aware that this might not be want. If the first list contains
0, andfirstIDis null, the result will be true, because you pass in 0 when the value is null.The Cast version of the expression returns false whenfirstIDis null.