C# 使用LINQ when my where子句将包含一个列表
我有一个我正试图使用LINQ查询的列表。T类型有一个属性,它是一个列表。我试图查询我的列表C# 使用LINQ when my where子句将包含一个列表,c#,linq,C#,Linq,我有一个我正试图使用LINQ查询的列表。T类型有一个属性,它是一个列表。我试图查询我的列表的列表属性,以仅提取那些列表属性项与我为筛选而构建的单独列表中的项相匹配的对象。我的代码如下所示: 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 t
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
T类{
列出姓名;
}
U类{
}
//然后我想通过询问哪个T objects的Names属性具有与我创建的列表相同的项来查询T的列表。
列出搜索项;
列出所有对象;
//查询AllObject并找出哪些对象的Name属性项与searchTermItems列表中的项匹配
您可以使用可枚举的.Intersect
:
var filtered = allObjects.Intersect(searchTermItems);
var filtered = allObjects.Where(x => x.Names.Intersect(searchTermItems).Any());
由于您使用的是列表集合而不是单个列表,因此为了获得所需的输出,您需要将Enumerable.Where
与Enumerable.Intersect
结合使用:
var filtered = allObjects.Intersect(searchTermItems);
var filtered = allObjects.Where(x => x.Names.Intersect(searchTermItems).Any());
allObjects
列表中没有可以像这样相交的属性-不过应该在其中相交。类似于Where(t=>t.Names.Intersect(searchTermItems.Count()>0)
编译器抱怨我使用了一个列表,并且我需要为Intersect使用IQueryable。我需要在这里做什么?我是否需要将父级和属性列表强制转换为iquirable?