簡體   English   中英

如何使用 EF PLUS 過濾孫輩

[英]How to filter grand children using EF PLUS

我想使用 Entityframework Plus 訪問Parent及其唯一活躍的Children和活躍的Grand Children

關系
父級 -> 子級 -> GrandChildren

var parent = await _dbContext.Parent
             .IncludeFilter(p=>p.Children.Where(c=>c.IsActive == true))
             .IncludeFilter(p=>p.Children.Select(c=>c.GrandChildren.Where(gc=>gc.IsActive ==true)))
             .Where(p=>p.ParnetID == 1234)
             .SingleOrDefaultAsync()

上面的查詢不起作用。 孩子不會被過濾。 它返回所有子項,包括非活動子項。 然而 GrandChildren 被過濾了(但是我猜大 childeren 在內存中被過濾而不是在 sql 中)

第二次使用IncludeFilter ,您必須在 Children 上也包括過濾器,否則,您將包括未過濾的 Children。

var parent = await _dbContext.Parent
             .IncludeFilter(p=>p.Children.Where(c=>c.IsActive == true))
             .IncludeFilter(p=>p.Children.Where(c=>c.IsActive == true).Select(c=>c.GrandChildren.Where(gc=>gc.IsActive ==true)))
             .Where(p=>p.ParnetID == 1234)
             .SingleOrDefaultAsync()

這就是我最終使用的。 這將創建 3 條 sql 語句

var parent = await _dbContext.Parent
             .IncludeFilter(p=>p.Children.Where(c=>c.IsActive == true).Select(c=>c.GrandChildren.Where(gc=>gc.IsActive ==true)))
             .Where(p=>p.ParnetID == 1234)
             .SingleOrDefaultAsync()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM