Reputation: 607
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
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
Reputation: 108947
var idCollection = new[] {'2', '3'};
var result = entitiescollection.Where(e => idCollection.Contains(e.Id));
Upvotes: 2