我有一个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 list
Run Code Online (Sandbox Code Playgroud)
你可以使用Enumerable.Intersect:
var filtered = allObjects.Intersect(searchTermItems);
Run Code Online (Sandbox Code Playgroud)
因为您正在处理列表集合而不是单个列表,所以为了获得所需的输出,您需要Enumerable.Where结合使用Enumerable.Intersect:
var filtered = allObjects.Where(x => x.Names.Intersect(searchTermItems).Any());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4143 次 |
| 最近记录: |