3

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.)

Arjan Einbu
  • 13,543
  • 2
  • 56
  • 59

1 Answers1

4

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));
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335