首页 > 解决方案 > 在 ThenInclude 上过滤,使用 EntityFrameworkPlus IncludeFilter

问题描述

我正在尝试向下过滤三个子级别,并仅找到 PropertyMailingAddress.Status== True 的子元素。

如何将过滤器向下转换三个级别并使用 EntityFrameworkPlus IncludeFilter 进行嵌套过滤?最有效的方法是什么?

类结构嵌套如下:

  1. 财产
  2. 财产党
  3. 聚会
  4. 聚会邮寄地址
  5. PropertyMailingAddress <--- 状态应该等于真(任何状态 == False 的孙子 PropertyMailingAddress 节点,都应该从此嵌套的孙子分支中删除,保留为 True 的 PropertyMailingAddress 节点)

这种原始方式不起作用:

var result = await db.Property.Include(pm => pm.PropertyParty)
                    .Include(pm => pm.PropertyParty)
                    .ThenInclude(x => x.Party)
                    .ThenInclude(x => x.PartyMailingAddress)
                    .ThenInclude(x => x.PropertyMailingAddress)
                    .Where(a => a.PropertyParty.Any(x => (x.Party.PartyMailingAddress.Any(z => z.PropertyMailingAddress.Any(h => h.Status == true))))).ToListAsync();

尝试使用 Entity Framework Plus 的有效方法:最后一行是否必须重新链接并再次与上面的嵌套 wheres 连接,或者是否可以?

var result = await db.Property.Include(pm => pm.PropertyParty)
                    .Include(pm => pm.PropertyParty)
                    .ThenInclude(x => x.Party)
                    .ThenInclude(x => x.PartyMailingAddress)
                    .ThenInclude(x => x.PropertyMailingAddress)
                    .IncludeFilter(y => y.PropertyMailingAddress.Where(z => z.Status == true)).ToListAsync();

*我们将需要所有嵌套实体,同时过滤,

目前使用的是 Net Core 2.2

标签: c#.netlinq.net-coreentity-framework-plus

解决方案


您不能IncludeIncludeFilter.

在 EF Core 中,IncludeFilter应该自动添加所有路径。

我们没有类定义,因此很难准确了解关系,但查询应该如下所示:

var result = await db.Property.IncludeFilter(pm => pm.PropertyParty
                                .Select(x => x.Party)
                                .SelectMany(x => x.PartyMailingAddress)
                                .SelectMany(x => x.PropertyMailingAddress.Where(z => z.Status == true)).ToListAsync();

过滤是在数据库中完成的。所以要小心 EF Core 2.x,因为他们在 EF Core 3.x 中删除了客户端评估,这导致了一些问题。

如果您需要更多帮助,只需在我们的问题跟踪器中提供可运行的解决方案:https ://github.com/zzzprojects/EntityFramework-Plus/issues


推荐阅读