Include
demands that the shape of the query doesn't change. It means that your query must return IQueryable<Match>
. GroupBy
operator is probably considered as shape changing because it returns IQueryable<IGrouping<TKey, TSource>>
. Once the shape of the query changes all Include statements are omitted. Because of that you cannot use Include
with projections, custom joins and groupings.
As a workaround you can execute grouping in Linq-to-objects:
var matches = context.matches
.Include("Official")
.Include("Slot")
.Include("MatchParticipants.Team")
.Include("Category.Tournament")
.Where(m => m.Category.Tournament.TournamentId == tournamentId)
.ToList()
.GroupBy(m => m.Category);
Edit: As mentioned in comments and in other answer, this is very dangerous workaround which can lead to performance problems. It pulls all records from database to the application and makes the aggregation in the app. It can work in same cases but it is definitely not applicable as generic solution.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…