How to select all objects that have a property value in list of values?

2.7k Views Asked by At

I have a table named Items. Items have a property named "locationId" Given a list of location Ids, how do I select all items?

List example

List<long> locationIds = new List<long> { 1, 2, 3 };

Essentially the query below, but for multiple locations at once:

var sleectedItems= db.Items.Select(i => i.LocationId == 2);
1

There are 1 best solutions below

7
On BEST ANSWER

You need to use Where with Contains:

var selectedItems = db.Items.Where(x => locationIds.Contains(x.LocationId));