如何在分组实体 LINQ 查询中包含相关对象?

发布于 2024-11-07 07:15:26 字数 1427 浏览 0 评论 0原文

我有一些与设置描述相关的设置实体,而设置描述又与设置组相关。 通过将“修改”字段作为键的一部分来保存设置历史记录。 要获取与特定类别匹配的设置,我使用此查询(这里的帮助):

    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 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 = 
        context.SettingDescriptions.Include("SettingGroup")
               .Where(x => x.Category == category)
               .ToArray();

为什么“独立”组查询中包含被忽略?

我可以将设置和描述加载合并到单个查询中吗?

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 = 
        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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2024-11-14 07:15:26

EF 团队的 AlexJ 发布了一系列精彩的提示,其中包括:

“提示 22 - 如何使 Include 真正包含”
http://blogs.msdn.com/b/alexj/archive/2009/06/02/tip-22-how-to-make-include-really-include.aspx

在我看来,你的查询返回“设置”实体(没有“形状更改”),因此应应用此提示。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文