c# - Linq 搜索原理和带有 OR 语句的子集合

标签 c# linq ef-core-2.1

我有一个父实体,它有一个子实体的ICollection。 我想使用 OR 搜索父集合和子集合。 因此,如果 parentEntity.Name 或任何 childrentEntity.PropertyValue 包含 searchTerm,则返回父实体。

我假设 SelectMany 会将子项展平并允许我轻松地搜索它们。 我还尝试“链接”我的查询,但这过滤了结果并且效果不佳 - 我需要 OR 表达式。

我的查询如下所示

var result = from v in parentEntity
             where v.Name.Contains(searchTerm)
             || v.ChildCollection.SelectMany(x => 
                         x.PropertyValue.Contains(searchTerm))
             select v;

最佳答案

我认为您正在寻找Any扩展方法:

var result = from v in parentEntity
             where v.Name.Contains(searchTerm)
             || v.ChildCollection.Any(x => 
                         x.PropertyValue.Contains(searchTerm))
             select v;

关于c# - Linq 搜索原理和带有 OR 语句的子集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52895028/

相关文章:

asp.net-core - 获取错误 : Entity type 'Course' is defined with a single key property,,但 2 个值已传递给 'DbSet.Find' 方法

c# - 对 DLL 入口点感到困惑(入口点未找到异常)

c# - 诊断在 Azure SDK 2.5.1 的 Web 角色中不起作用

c# - 桌面上的透明表格

c# - Where 子句中包含 ".Any"的动态 Linq (C#/.Net Core/EF Core)

c# - Entity Framework Core 添加迁移失败

c# - Entity Framework EDMX 到代码优先

sql - 关于 LINQ 最高性能方法的思考

c# - 使用 linq 对 XMLDocument 进行排序

c# - 如何使用 Distinct 从 Linq 查询中选择特定对象?