Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to check if ID exists in List

I am using LINQ Entity framework. I have a SQL table and I want to get all the items in the table that have an ID that exist in a List

Is this possible with LINQ?

like image 462
Michael Avatar asked Jan 20 '12 06:01

Michael


2 Answers

Yes, it is possible.

(from item in yourContext.YourTable where yourList.Contains(item.ID) select item).ToList();
like image 117
Haris Hasan Avatar answered Oct 16 '22 12:10

Haris Hasan


You can do this with Contains its translated into sql IN:

context.SomeTable.Where(r => someListOfId.Contains(r.ID));
like image 34
Piotr Auguscik Avatar answered Oct 16 '22 13:10

Piotr Auguscik