I want to generate this SQL statement in LINQ:
select * from Foo where Value in ( 1, 2, 3 )
The tricky bit seems to be that Value is a column that allows nulls.
The equivalent LINQ code would seem to be:
IEnumerable<Foo> foos = MyDataContext.Foos;
IEnumerable<int> values = GetMyValues();
var myFoos = from foo in foos
where values.Contains(foo.Value)
select foo;
This, of course, doesn’t compile, since foo.Value is an int? and values is typed to int.
I’ve tried this:
IEnumerable<Foo> foos = MyDataContext.Foos;
IEnumerable<int> values = GetMyValues();
IEnumerable<int?> nullables = values.Select( value => new Nullable<int>(value));
var myFoos = from foo in foos
where nullables.Contains(foo.Value)
select foo;
…and this:
IEnumerable<Foo> foos = MyDataContext.Foos;
IEnumerable<int> values = GetMyValues();
var myFoos = from foo in foos
where values.Contains(foo.Value.Value)
select foo;
Both of these versions give me the results I expect, but they do not generate the SQL I want. It appears that they’re generating full-table results and then doing the Contains() filtering in-memory (ie: in plain LINQ, without -to-SQL); there’s no IN clause in the DataContext log.
Is there a way to generate a SQL IN for Nullable types?
NOTE
As it turns out, the problem I was having didn’t have anything to do Contains or Nullable, and so the phrasing of my question is largely irrelevant. See @Nick Craver’s accepted answer for details.
This should work for your example:
Casting as an
IEnumerable<T>from the start means execution will be outside of SQL, instead call theIQueryable<Foo>directly. If you cast asIEnumerableand use it in a query, it gets allMyDataContext.Foosthen uses that iterator and executes the rest of the query in C# instead of in SQL.If you want to run in SQL, don’t cast as
IEnumerableanywhere along the way. The effect is the same as usingMyDataContext.Foos.AsEnumerable()in the query.