LINQ-to-SQL IN/Contains() for Nullable<T>
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:
IEnumerable<int> values = GetMyValues();
var myFoos = from foo in MyDataContext.Foos;
where values.Contains(foo.Value.Value)
select foo;
Casting as an IEnumerable<T>
from the start means execution will be outside of SQL, instead call the IQueryable<Foo>
directly. If you cast as IEnumerable
and use it in a query, it gets all MyDataContext.Foos
then 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 IEnumerable
anywhere along the way. The effect is the same as using MyDataContext.Foos.AsEnumerable()
in the query.
精彩评论