I believe you should be able to take a collection of ids and use the
.Contains() extension method and EF will convert it into an IN clause. i.e.
var ids = new[] { 1, 5, 7, 9 };
var matches = from t in db.Things where ids.Contains(t.Id) select t.Id;
should be converted to
SELECT Id
FROM Thing
WHERE Id IN (1, 5, 7, 9)
Or at least, that's what Linq2Sql does. Actually what it does is
SELECT Id
FROM Thing
WHERE Id IN (@p0, @p1, @p2, @p3)
and then passes in the Ids as parameters. I'm pretty certain that if this
list of Ids gets large enough EF will pull the whole set of Things down and
do the test in memory. You'd need to verify it's behavior for your scenario.
Regards,
Mike
On Fri, Jul 27, 2012 at 9:38 AM, Bill McCarthy <
[email protected]> wrote:
> Wouldn't a simple Join do what you want ?
>
>
> |-----Original Message-----
> |From: [email protected] [mailto:ozdotnet-
> |[email protected]] On Behalf Of Arjang Assadi
> |Sent: Friday, 27 July 2012 11:08 AM
> |To: ozDotNet
> |Subject: Re: LINQ question
> |
> |I have a question similar to Greg's, but instead of one id, what is the
> best way to
> |deal with a list of Id's and return everything that matched?
> |Of course one can use a for loop for the id's, but is there a way to deal
> with list of
> |id's in one go? What would the generated Sql look like?
> |
> |
> |Regards
> |
> |Arjang
>
>