0

I want get to Parent and its only active Children and active Grand Children using Entityframework Plus

Relationship
Parent -> 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()

The above query does not work. The children does not get filtered. It returns all Children including inactive children. However GrandChildren gets filtered( however i am guessing grand childeren are getting filtered in memory not in sql)

2 Answers 2

1

You must include the filter as well on the Children the second time you use IncludeFilter, otherwise, you will include the Children unfiltered.

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()
1
  • Thats what i ended up using except you don't need the first filter
    – LP13
    Feb 27, 2020 at 15:50
0

This is what i have ended up using. This create 3 sql statements

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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.