Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Include in following query does not include really

var diaryEntries = (from entry in repository.GetQuery<OnlineDiary.Internal.Model.DiaryEntry>()
                               .Include("DiaryEntryGradeChangeLog")
                               .Include("DiaryEntryAction")
                                           join diary in repository.GetQuery<OnlineDiary.Internal.Model.OnlineDiary>()                                           
                                           on entry.DiaryId equals diary.Id
                                           group entry
                                           by diary
                                           into diaryEntriesGroup
                                           select new { Diary = diaryEntriesGroup.Key,
                                               DiaryEntry = diaryEntriesGroup.OrderByDescending(diaryEntry => diaryEntry.DateModified).FirstOrDefault(),
                                           });

This query does not include "DiaryEntryGradeChangeLog" and "DiaryEntryAction" navigation properties, what is wrong in this query?

I have removed join from the query and corrected as per below, and still it populates nothing

var diaryEntries = from entry in repository.GetQuery<OnlineDiary.Internal.Model.DiaryEntry>()
                                   .Include("DiaryEntryGradeChangeLog").Include("DiaryEntryAction")
                                   .Where(e => 1 == 1)
                                       group entry
                                       by entry.OnlineDiary
                                       into diaryEntryGroups
                                       select 
                                       new { DiaryEntry = diaryEntryGroups.OrderByDescending(diaryEntry => diaryEntry.DateModified).FirstOrDefault() };
like image 554
Nirman Avatar asked Sep 16 '25 06:09

Nirman


1 Answers

It will not. Include works only if the shape of the query does not change (by design). If you use this query it will work because the shape of the query is still same (OnlineDiary.Internal.Model.DiaryEntry):

var diaryEntries = (from entry in repository.GetQuery<OnlineDiary.Internal.Model.DiaryEntry>()
                           .Include("DiaryEntryGradeChangeLog")
                           .Include("DiaryEntryAction");

But once you use manual join, grouping or projection (select new { }) you have changed the shape of the query and all Include calls are skipped.

Edit:

You must use something like this (untested) to get related data:

var diaryEntries = from entry in repository.GetQuery<OnlineDiary.Internal.Model.DiaryEntry>()
                   group entry by entry.OnlineDiary into diaryEntryGroups
                   let data = diaryEntryGroups.OrderByDescending(diaryEntry => diaryEntry.DateModified).FirstOrDefault()
                   select new { 
                       DiaryEntry = data,
                       GradeChangeLog = data.DiaryEntryGradeChangeLog,
                       Action = data.DiaryEntryAction
                   };

or any similar query where you manually populate property for relation in projection to anonymous or unmapped type.

like image 68
Ladislav Mrnka Avatar answered Sep 19 '25 15:09

Ladislav Mrnka