VSO
VSO

Reputation: 12646

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

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);

Upvotes: 2

Views: 2894

Answers (1)

haim770
haim770

Reputation: 49095

You need to use Where with Contains:

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

Upvotes: 6

Related Questions