views:

214

answers:

1

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.

+5  A: 

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.

Nick Craver
Wow, I didn't realize that casting the references made a difference (undoubtedly due to the use of specifically-typed extension methods). That's unfortunate from a OOP point of view. However, it did solve my problem. Thanks!
Craig Walker