Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 468
Ajax3.14 Avatar asked Feb 12 '14 14:02

Ajax3.14


1 Answers

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();
like image 96
Fredrik Ljung Avatar answered Nov 06 '22 08:11

Fredrik Ljung