我想使用Entityframework Plus來了解“ Parent
及其唯一的活躍Children
和活躍Grand Children
關係
父母->子女->孫子
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被過濾了(但是我猜測大子級在內存中而不是在SQL中被過濾)
第二次使用IncludeFilter
,還必須在子級上包括過濾器,否則,您將包括未過濾的子級。
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()