c# - 当我的 where 子句将包含 LIST 时使用 LINQ

标签 c# linq

我有一个要使用 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.WhereEnumerable.Intersect< 结合使用:

var filtered = allObjects.Where(x => x.Names.Intersect(searchTermItems).Any());

关于c# - 当我的 where 子句将包含 LIST 时使用 LINQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16555351/

相关文章:

c# - 如何将这些类似的 linq 查询合并为一个?

c# - XNA - 缺少视频和视频播放器的命名空间?

c# - Surface 2.0 SDK 还在使用吗?

c# - 绑定(bind)数组+一个对象到Dropdownlist

c# - 由于对 Microsoft.Bcl 的依赖,TeamCity 上的 CI 构建失败

c# - 在 Netduino 板上使用 .NET Micro Framework 提高时间分辨率(用于调暗 LED)?

c# - 并行内连接

c# - 将 Linq 表达式添加到表达式列表的方法

C# Linq to SQL 无效转换异常

c# - 如果 Rx 触发另一个事件,则忽略事件