当我的where子句包含LIST时使用LINQ

时间:2013-05-15 01:30:57

标签: c# linq

我有一个List,我试图使用LINQ进行查询。 T类型具有List<的属性。 U>。我正在尝试查询我的列表< T>列表< U>属性仅拉取那些列表属性项与单独列表中的项匹配的对象< 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 list

1 个答案:

答案 0 :(得分:6)

您可以使用Enumerable.Intersect

var filtered = allObjects.Intersect(searchTermItems);

由于您使用的是列表集合而不是单个列表,因此为了获得所需的输出,您需要将Enumerable.WhereEnumerable.Intersect结合使用:

var filtered = allObjects.Where(x => x.Names.Intersect(searchTermItems).Any());
相关问题