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

时间:2017-12-20 19:41:00

标签: c# entity-framework linq

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

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

这会返回错误:

  

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

有简单的方法吗?

2 个答案:

答案 0 :(得分: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>();

答案 1 :(得分:0)

这些Include生成联接,过滤掉使用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>();