.include 在以下查询中不包括真的 [英] .Include in following query does not include really

查看:35
本文介绍了.include 在以下查询中不包括真的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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() };

解决方案

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.

这篇关于.include 在以下查询中不包括真的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆