Reputation: 1391
I have two different lists where one is a bunch of ID's as in a List<int> idsList
, the other however is a list of objects like List<MyObject> myObjectList
where the object looks like this:
class MyObject{
private List<int> ids;
public MyObject(List<int> ids){
this.ids = ids;
}
public List<int> Ids{
get{
return ids;
}
}
}
As you can see each object can contain one or multiple IDs (never zero or null ids). So what I need at the end is to know what objects in myObjectList have any id(s) from my idsList. So far if I do:
var ids = from g in onScreen where g.Ids.Contains(myIntVariable) select g;
it would give me a list of the object(s) that contain myIntVariable. What I do not know how to do is to match the content of the idsList with the list in MyObject. Thanks!
Upvotes: 0
Views: 119
Reputation: 2053
Assuming g
is your object list and idsList
is your int list:
foreach (var myObject in g.Where( obj => obj.Ids.Any( itemId => idsList.Contains(itemId) ) )) {
//Use your myObject here
}
Hope it works,
Upvotes: 0
Reputation: 37719
One way to go:
var listOfMyObjectsContainingAnIdFromIdsList = myObjectList.Where(myObject => myObject.Ids.Any(id => idsList.Contains(id)));
Upvotes: 1