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

此查询不包括“DiaryEntryGradeChangeLog”和“DiaryEntryAction”导航属性,此查询有什么问题?

我已经从查询中删除了联接并按照下面的方法进行了更正,但它仍然没有填充任何内容

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

1 回答 1

2

它不会。Include仅当查询的形状不改变时才有效(按设计)。如果您使用此查询,它将起作用,因为查询的形状仍然相同(OnlineDiary.Internal.Model.DiaryEntry):

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

但是,一旦您使用手动连接、分组或投影 ( select new { }),您就改变了查询的形状,所有Include调用都将被跳过。

编辑:

您必须使用这样的东西(未经测试)来获取相关数据:

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

或任何类似的查询,您手动填充投影到匿名或未映射类型的关系的属性。

于 2013-01-09T10:26:15.037 回答