c# - 在 ThenInclude 上过滤,使用 EntityFrameworkPlus IncludeFilter
问题描述
我正在尝试向下过滤三个子级别,并仅找到 PropertyMailingAddress.Status== True 的子元素。
如何将过滤器向下转换三个级别并使用 EntityFrameworkPlus IncludeFilter 进行嵌套过滤?最有效的方法是什么?
类结构嵌套如下:
- 财产
- 财产党
- 聚会
- 聚会邮寄地址
- 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
解决方案
您不能Include
与IncludeFilter
.
在 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
推荐阅读
- javascript - 如何显示一天到当前日期?角2+?
- javascript - window.screen.width 和 height 是以 CSS 像素还是实际屏幕像素返回的值?
- apache - 带有否定条件规则的 RewriteCond?
- node.js - 完成其他动作后显示 index.html
- c# - 使用 MemoryStream 写入大文件时出现 OutOfMemory 异常
- functor - Dhall - 记录字段上的地图
- php - array_merge():参数 #2 不是数组 [WP Plugin - Event Booking Pro]
- java - ThreadPoolTaskExecutor 抛出 RejectedExecutionException
- swift - 使用两个单独的值对 tableview 中的 JSON 数据进行排序 | 迅速
- html - 引导卡中的绝对中间值