我有一个List,我试图使用LINQ查询。 T类型具有List <U>的属性。 我试图查询我的List <T>的List <U>属性,只拉取那些List属性项与我为过滤构建的单独List <U>中的项匹配的对象。 我的代码如下所示:
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 listI 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最满意答案
你可以使用Enumerable.Intersect :
var filtered = allObjects.Intersect(searchTermItems);因为您正在处理列表集合而不是单个列表,所以为了实现所需的输出,您需要使用Enumerable.Where与Enumerable.Intersect一起使用:
var filtered = allObjects.Where(x => x.Names.Intersect(searchTermItems).Any());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());更多推荐
发布评论