Include and ThenInclude not working after groupBy with OrderBy
Core 2.0 AspNetCore.All 2.1.0-preview1-26462 Navigation property ICollection is null, Core 1.1 work fine
var userCourseQuery = Unit.DbContext.UserCourses
.Include(_x => _x.ClassroomSessions)
.Include(_x => _x.Course)
.Include(_x => _x.User)
.ThenInclude(_x => _x.UserCompanies)
.ThenInclude(_x => _x.Company)
.GroupBy(_x => _x.IdUser)
.OrderBy(_x => _x.Key)
.ToList();
Includes null
.GroupBy(_x => _x.IdUser) // <- If comment this work fine`
I've just encountered this problem myself, upgrading from EFCore 1.0 to 2.0.
An included navigation property works after a GroupBy, but when you add an OrderBy on the group, the included properties are ignored, and are null in the results.
This happens because when order by is introduced after the groupby, the groupby itself is pushed into the subquery. QM looks as follows:
from IGrouping<string, Customer> e in
(from Customer c in DbSet<Customer>
select [c]).GroupBy([c].Name, [c])
order by [e].Key asc
select [e]
This causes issues, because Include compiler during ApplyIncludeExpressionsToQueryModel phase only looks at top level group bys.
fixed in bc567fb07f9bee6f317032cc4906b7b614facd2d
This bug is still present in EF Core 2.1.4.
@psyren89 Please file a new issue with a runnable project/solution or complete code listing that demonstrates the behavior you are seeing.