.Include in following query does not include really

后端 未结 1 1094
南旧
南旧 2021-01-22 20:50
var diaryEntries = (from entry in repository.GetQuery()
                               .Include(\"DiaryEntryGradeChangeLog\"         


        
1条回答
  •  醉梦人生
    2021-01-22 21:24

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

    0 讨论(0)
提交回复
热议问题