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이 필터링됩니다 (그러나 grand 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()