Reputation: 195
I have a List that I am trying to query using LINQ. The T type has a property that is a List < U >. I am trying to query my List < T >'s List < U > property to pull only those objects who's List property items match the items in a seperate List < U > that I have built for filtering. My code looks like this:
class T {
List<U> Names;
}
class U {
}
//then I want to query a List of T by interrogating which T objects' Names property has the same items that I have a List < U > that I have created.
List<U> searchTermItems;
List<T> allObjects;
//Query allObjects and find out which objects' Name property items match the items in the searchTermItems list
Upvotes: 3
Views: 4232
Reputation: 7804
You could use Enumerable.Intersect
:
var filtered = allObjects.Intersect(searchTermItems);
Because you are working with a collection of lists rather than a single list, in order to acheive the desired output you will need to use Enumerable.Where
in conjunction with Enumerable.Intersect
:
var filtered = allObjects.Where(x => x.Names.Intersect(searchTermItems).Any());
Upvotes: 6