当where子句包含LIST时使用LINQ
本文关键字:LINQ LIST where 子句 包含 | 更新日期: 2023-09-27 18:13:50
我有一个列表,我试图使用LINQ查询。T类型有一个属性是List 。我正试图查询我的列表
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());