我想使用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()