开发者

How to include related objects in grouped Entity LINQ queries?

I have some Setting entities that are related to a SettingDescription which is related to a SettingGroup. Setting history is preserved by making a "Modified" field part of the key. To get the settings matching a specific category I use this query (after help from here):

    var latestSettings =
        context.Settings.Include("Description.SettingGroup")
            .OrderByDescending(x => x.Modified)
            .GroupBy(x => 
                new { 
                    x.Category, 
                    x.Group, 
                    x.Name, 
                    x.Target }, x => x)
            .Where(x => x.Key.Category == category)
            .Select(result => result.FirstOrDefault())
            .ToArray();

This returns a set of the latest settings, but the "Include" part is completely ignored. However, I can force load the descriptions by running a second dummy query that loads the descriptions into the context.

    var latestSettings =
        context.Settings.Include("Description.SettingGroup")
            .OrderByDescending(x => x.Modified)
            .GroupBy(x => 
                new { 
                    x.Category, 
                    x.Group, 
                    x.Name, 
                    x.Target }, x => x)
            .Where(x => x.Key.Category == category)
            .Select(result => result.FirstOrDefault())
            .ToArray();
    var settingDescriptions开发者_JAVA技巧 = 
        context.SettingDescriptions.Include("SettingGroup")
               .Where(x => x.Category == category)
               .ToArray();

Why is the include ignored in the "stand alone" group query?

Can I combine the setting and description loading into a single query?


AlexJ from the EF team posted an excellent series of tips, including:

"Tip 22 - How to make Include really Include"
http://blogs.msdn.com/b/alexj/archive/2009/06/02/tip-22-how-to-make-include-really-include.aspx

It looks to me like your query is returning "Settings" entities (without a "change of shape") so this tip should apply.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜