我有一个要使用 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 list
最佳答案
你可以使用Enumerable.Intersect
:
var filtered = allObjects.Intersect(searchTermItems);
因为您正在处理一组列表而不是单个列表,为了获得所需的输出,您需要将 Enumerable.Where
与 Enumerable.Intersect< 结合使用
:
var filtered = allObjects.Where(x => x.Names.Intersect(searchTermItems).Any());
关于c# - 当我的 where 子句将包含 LIST 时使用 LINQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16555351/