使用EntityFrameworkPlus IncludeFilter在ThenInclude上进行过滤 [英] Filtering On ThenInclude, with EntityFrameworkPlus IncludeFilter
问题描述
我试图向下过滤三个子级别并仅查找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?
类结构嵌套如下:
- 属性
- PropertyParty
- Party
- PartyMailingAddress
- PropertyMailingAddress < ---状态应等于true(任何状态== False的孙子PropertyMailingAddress节点,应从此嵌套子孙分支中删除,并保持PropertyMailingAddress节点为True)
- Property
- PropertyParty
- Party
- PartyMailingAddress
- PropertyMailingAddress <--- Status should equal true (Any grandchild PropertyMailingAddress nodes with Status == False, should be removed from this nested grandchild branch, keep the PropertyMailingAddress nodes which are True)
这种原始方式无效:
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尝试有效的方式:
是否必须重新链接最后一行还是嵌套在上面还是下面?
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
。
在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,因为他们有一个客户端ev它们在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上进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!