Entity Framework: Get all rows from the table for the ids in list

entity-framework linq sql

Question

My situation is I have ids {2,10,16,24,32,...} and would like to get the rows that matches these ids from the table. How do I do it in Entity framework.

In SQL I can do something like:

SELECT * FROM table WHERE id IN (2,10,16,24,32)

How do achieve this in Entity framework?

1
5
2/12/2014 2:59:45 PM

Accepted Answer

You can shove your ids into a list and use that inside the Where to filter out only the rows in table whose id matches those in the list:

var ids = new List<int>() { 2, 10, 16, 24, 32 };
var rows = Table.Where(t => ids.Contains(t.id)).ToList();
10
2/12/2014 3:13:34 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow