实体框架6仅包括具有特定值的子记录? [重复]

问题描述 投票:0回答:2

这个问题在这里已有答案:

我正在尝试返回包含满足特定数据要求的子记录的记录集。这是我的基本查询,在这种情况下,我只是试图将IsActive字段为true的子项带回来。

var result = db.Projects
         .Include(p => p.Department)
         .Include(p => p.ProjectPhases.Where(pp =>pp.IsActive ))
         .Include(p => p.Notes)
         .AsQueryable<Project>();

这会返回一个错误:

Include路径表达式必须引用在类型上定义的导航属性。使用虚线路径作为参考导航属性,使用Select运算符作为集合导航属性。

有简单的方法吗?

c# entity-framework linq
2个回答
1
投票

你不能在Include里面执行where子句。你为什么不尝试这样做;

var result = db.Projects
         .Include(p => p.Department)
         .Include(p => p.ProjectPhases)
         .Include(p => p.Notes)
         .Where(x => x.ProjectPhases.Where(pp =>pp.IsActive))
         .AsQueryable<Project>();

0
投票

那些Includes生成连接,过滤掉使用Where链后的Include

var result = db.Projects
         .Include(p => p.Department)
         .Include(p => p.ProjectPhases)
         .Include(p => p.Notes)
         .Where(it => it.ProjectPhases.All(a=>a.IsActive))
         .AsQueryable<Project>();
© www.soinside.com 2019 - 2024. All rights reserved.