使用复合键在表中进行 LINQ 子选择

发布于 2024-11-06 23:51:48 字数 379 浏览 0 评论 0原文

我在数据库中有一个通过实体框架查询访问的设置表。 这些设置有一个复合键:类别、组、名称、目标、修改。 这些设置显然还包含非关键字段,例如值。

我如何编写一个查询来为我提供相同的最新设置(类别、组、名称、目标)?

更具体地说,我想要所有最新的设置实体,包括与特定类别、组、目标组合匹配的非关键字段。

例如:向我提供类别为“数据库”且组为“超时”的所有目标的所有最新设置。

这可以分两步完成,初始查询返回所有匹配条件的 [Category, Group, Name, Target, Modified] 键,然后循环查询每个键的设置,但这会产生大量数据库查询,产生开销。

如果我想最大限度地减少框架执行的 SQL 数据库查询数量,我该如何使用 LINQ 来实现这一点?

I have a settings table in a database that is accessed through Entity Framework queries.
The settings have a composite key: Category, Group, Name, Target, Modified.
The settings obviously also contain non-key fields like value.

How would I write a query that gives me the latest setting within the same (Category, Group, Name, Target)?

More specific, I want all the latest setting entities, including the non-key fields, that matches a specific category, group, target combination.

For example: Give me all the latest settings for all targets where Category is "Database" and Group is "Timeout".

This could be done in two-steps with an initial query returning all [Category, Group, Name, Target, Modified] keys matching the criteria and then a loop querying a setting for each key, but that would produce a lot of database queries, creating overhead.

How would I do this with LINQ if I want to minimize the number of SQL database queries executed by the framework?

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

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

发布评论

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

评论(2

对你的占有欲 2024-11-13 23:51:48
var query = from s in db.Settings
            group s by new
            {
              s.Category,
              s.Group,
              s.Name,
              s.Target,
            } into sg
            select new
            {
              Setting = sg.OrderByDescending(r => r.Modified).FirstOrDefault()
            };

编辑 - 如果您想返回设置列表

 List<Setting> list = (from s in db.Settings
                       group s by new
                       {
                         s.Category,
                         s.Group,
                         s.Name,
                         s.Target,
                       } into sg
                       select sg.OrderByDescending(r => r.Modified).FirstOrDefault()
                      ).ToList();
var query = from s in db.Settings
            group s by new
            {
              s.Category,
              s.Group,
              s.Name,
              s.Target,
            } into sg
            select new
            {
              Setting = sg.OrderByDescending(r => r.Modified).FirstOrDefault()
            };

Edit - If you want to return a List of Settings

 List<Setting> list = (from s in db.Settings
                       group s by new
                       {
                         s.Category,
                         s.Group,
                         s.Name,
                         s.Target,
                       } into sg
                       select sg.OrderByDescending(r => r.Modified).FirstOrDefault()
                      ).ToList();
你爱我像她 2024-11-13 23:51:48

尝试使用以下 LINQ 语句。

        List<Settings> latestSettings = 
            db.Settings.OrderByDescending(x => x.Modified).GroupBy(x => new {x.Category, x.Group, x.Name, x.Target}, x => x)
            .Select(result => result.FirstOrDefault())
            .ToList();

Try to use the following LINQ Statement.

        List<Settings> latestSettings = 
            db.Settings.OrderByDescending(x => x.Modified).GroupBy(x => new {x.Category, x.Group, x.Name, x.Target}, x => x)
            .Select(result => result.FirstOrDefault())
            .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文