Arjan Einbu
Arjan Einbu

Reputation: 13692

How can I use Linq to join between objects and entities?

I have a collection of IDs in memory, and I would like to fetch only rows from a DB matching those IDs.

In SQL, I could either write a query like SELECT * FROM mytable WHERE id IN (1,3,5,10) or do a join between tables.

My problem is that EF can't build a query where I join my EF-data with my local array or list.

(I'm using EF4.1, but I'm guessing the problem/solution would be similar in older versions, as well as with Linq-to-SQL.)

Upvotes: 3

Views: 636

Answers (1)

BrokenGlass
BrokenGlass

Reputation: 161002

You can use Contains() with your ID collection myIDs to generate the equivalent WHERE id IN .. query:

var results = context.mytable.Where(x => myIds.Contains(x.Id));

Upvotes: 4

Related Questions