user738383
user738383

Reputation: 607

Selecting entities from an array

I have a collection of IDs, and a collection of entities with IDs.

What I want to do is build a single query that finds those records which correspond to the IDs in the first collections.

So, for example, the collection of IDs contains '2' and '3', and the collection of entities contains entities with IDs from one to ten. I'd like to get at records with IDs which are '2' and '3'.

Can this be done with a single query? If so, how?

Thanks.

Upvotes: 2

Views: 444

Answers (2)

James Hill
James Hill

Reputation: 61792

Use Contains()

Option 1

List<int> myFilter = new List<int>() { 2, 3 };
var myResults = from foo in myEntities
                where myFilter.Contains(foo.ID)
                select foo;

Option 2

List<int> myFilter = new List<int>() { 2, 3 };
var myResults = myEntities.Where(x => myFilter.Contains(x.ID));

Upvotes: 5

Bala R
Bala R

Reputation: 108947

var idCollection = new[] {'2', '3'};
var result = entitiescollection.Where(e => idCollection.Contains(e.Id));

Upvotes: 2

Related Questions