【问题标题】:Filtering On ThenInclude, with EntityFrameworkPlus IncludeFilter在 ThenInclude 上过滤,使用 EntityFrameworkPlus IncludeFilter
【发布时间】:2020-05-27 01:13:55
【问题描述】:

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

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

类结构嵌套如下:

  1. 财产
  2. 物业方
  3. 聚会
  4. 派对邮寄地址
  5. 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# .net linq .net-core entity-framework-plus


【解决方案1】:

您不能将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

【讨论】:

  • 嗨,乔恩,我需要相关实体,答案中有过滤器,您的答案只包含过滤器,谢谢
猜你喜欢
  • 2021-10-01
  • 2018-04-21
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
相关资源
最近更新 更多