efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Include and ThenInclude not working after groupBy with OrderBy

Open AniMeIIIkA opened this issue 9 years ago • 5 comments

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`

AniMeIIIkA avatar Aug 04 '17 05:08 AniMeIIIkA

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.

JimLynn avatar Aug 16 '17 02:08 JimLynn

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.

maumar avatar Jan 25 '18 01:01 maumar

fixed in bc567fb07f9bee6f317032cc4906b7b614facd2d

maumar avatar Feb 03 '18 01:02 maumar

This bug is still present in EF Core 2.1.4.

psyren89 avatar Oct 31 '18 03:10 psyren89

@psyren89 Please file a new issue with a runnable project/solution or complete code listing that demonstrates the behavior you are seeing.

ajcvickers avatar Oct 31 '18 15:10 ajcvickers