问题描述
我正在尝试向下过滤三个子级别并仅查找 PropertyMailingAddress.Status== True 的子元素.
I am trying to filter three child levels down and find only child elements where PropertyMailingAddress.Status== True.
如何将过滤器向下转换三级并使用 EntityFrameworkPlus IncludeFilter 进行嵌套过滤?最有效的方法是什么?
How do I convert filter three levels down and conduct nested filtering with EntityFrameworkPlus IncludeFilter? What is the most efficient way?
类结构嵌套如下:
- 财产
- 物业派对
- 派对
- 派对邮寄地址
- PropertyMailingAddress <--- Status 应该等于 true(任何具有 Status == False 的孙子 PropertyMailingAddress 节点都应该从这个嵌套的孙子分支中删除,保留为 True 的 PropertyMailingAddress 节点)莉>
这种原始方式不起作用:
This original way is not working:
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 再次连接,或者下面没问题?
Trying efficient way with Entity Framework Plus:Does the last line have to be relink joined again with nested wheres above, or is below fine?
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();
*我们将要求所有嵌套实体,同时过滤,
*We will require all the nested entities, while filtering,
目前使用的是 Net Core 2.2
Currently using Net Core 2.2
推荐答案
您不能将 Include
与 IncludeFilter
混合使用.
You cannot mixte Include
with IncludeFilter
.
在 EF Core 中,IncludeFilter
应自动添加所有路径.
In EF Core, the IncludeFilter
should automatically add all paths.
我们没有类定义,因此很难准确了解关系,但查询应如下所示:
We don't have the class definition so it makes it hard to know exactly the relationship but the query should look like this:
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 中删除了客户端评估,这导致了一些问题.
The filtering is done in the database. So be careful with EF Core 2.x, as their have a client side evaluation they removed in EF Core 3.x which was causing some problem.
如果您需要更多帮助,只需在我们的问题跟踪器中提供可运行的解决方案:https://github.com/zzzprojects/EntityFramework-Plus/issues
If you need more help, just provide a runnable solution in our issue tracker: https://github.com/zzzprojects/EntityFramework-Plus/issues
这篇关于使用 EntityFrameworkPlus IncludeFilter 过滤 ThenInclude的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!