如何包含相关实体但排除Deleted = true的记录?

时间:2016-08-06 03:45:32

标签: entity-framework

我有两个实体EntityTestEntityRelated

我想查询并获取两个Entites中两个Entted都不为真的Entites的结果。

我试过以下声明。它通过排除Deleted = true的记录返回EntityTest记录,但不过滤EntityRelated的记录。

db.EntityTest
.Include(i => i.EntityRelated)
.Where(x => x.Deleted != true && x.EntityRelated.Any(a => a.Deleted != true).ToList();

1 个答案:

答案 0 :(得分:0)

解决方案1:使用扩展方法Include2()Include2的此实现。如下:

db.EntityTest
    .Include(i => i.EntityRelated.Where(e=>e.Deleted == false))
    .Where(x => x.Deleted != true && x.EntityRelated.Any(a => a.Deleted != true)
    .ToList();

解决方案1: