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

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

问题描述

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导航属性,此查询有什么问题?

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


推荐答案

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

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.

编辑:

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

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.

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

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