LINQ to check if ID exists in List

45,081

Solution 1

Yes, it is possible.

(from item in yourContext.YourTable where yourList.Contains(item.ID) select item).ToList();

Solution 2

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

context.SomeTable.Where(r => someListOfId.Contains(r.ID));
Share:
45,081
Michael
Author by

Michael

C#, SharePoint, Angular/React software developer. Director at HotSnail

Updated on July 29, 2020

Comments

  • Michael
    Michael almost 4 years

    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?

  • Magnus
    Magnus over 12 years
  • Haris Hasan
    Haris Hasan over 12 years
    That post is 3 years old. I believe it does now
  • JSideris
    JSideris about 10 years
    @HarisHasan Hey, could you comment on the performance of this type of query versus creating a stored procedure that does an inner join against a table valued parameter?