Linq Search Principle and child collection with OR 语句
Linq Search Principle and child collection with OR statement
我有一个 parent 实体,它有一个 child 实体的 ICollection
。
我想使用 OR
搜索 parent 和 child 集合。
因此,如果 parentEntity.Name
或任何 childrentEntity.PropertyValue
包含 searchTerm
,则 return 是 parent 实体。
我假设 SelectMany
会压平 children 并允许我轻松地搜索它们。
我也尝试了 'chaining' 我的查询,但是过滤掉了结果并且效果不佳 - 我需要 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;
我有一个 parent 实体,它有一个 child 实体的 ICollection
。
我想使用 OR
搜索 parent 和 child 集合。
因此,如果 parentEntity.Name
或任何 childrentEntity.PropertyValue
包含 searchTerm
,则 return 是 parent 实体。
我假设 SelectMany
会压平 children 并允许我轻松地搜索它们。
我也尝试了 'chaining' 我的查询,但是过滤掉了结果并且效果不佳 - 我需要 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;