用SQL查询替换EF LINQ查询组包含组

发布于 2025-01-29 16:42:09 字数 1851 浏览 3 评论 0原文

我们有一个EF LINQ查询,与以下相同的连接和组和组。

    var subgroupquery1 = from sg in _dbContext.SubGroups
              join e in _dbContext.Admins.Where(e => e.Status.StartsWith("Active") && e.SubGroupCode != indivsubgroupcode)
                on sg.SubGroupCode equals e.SubGroupCode
              join mf in _dbContext.MasterSetups.Where(e => groupstoinclude.Contains(e.AdminGroup)).Select(mfs => new { mfs.RefCode})
                on e.EntCode equals mf.RefCode
              group e by sg into g
              select new SubGroupInfo
              {
                  SubGroupKey = g.Key.SubGroupCode,
                  SubGroupCode = g.Key.SubGroupCode,
                  SubGroupName = string.Concat(g.Key.Name, " (", g.Key.SubGroupCode, ")"),
                  MasterGroupCode = g.Key.MasterGroupCode,
                  NoOfEntities = g.Count(),
                  Managers = g.Select(x => x.Manager).Distinct(),
                  Officers = g.Select(x => x.Officer).Distinct(),
                  ClientType = g.Key.ClientType,
                  Coordinator = g.Key.Coordinator,
                  GroupDate = g.Key.Group_Date
              };

查询的表有大量列。 EF正在生成复杂的SQL查询,并不必要地选择所有列,使其缓慢而沉重。

我正在尝试将此LINQ查询转换为SQL查询,如下所示。但是我无法在分组后选择列。有人可以帮忙完成查询吗?

    SELECT sg.SubGroupCode, sg.MasterGroupCode, sg.ClientType, sg.BillingCoordinator, sg.Billing_Date, sg.[Name]
    FROM vw_W360_SubGroup sg
    JOIN vw_W360_EntityAdmin e ON sg.SubGroupCode = e.SubGroupCode
    JOIN vw_W360_MasterFileSetup mf ON e.EntCode = mf.RefCode
    GROUP BY sg.SubGroupCode, sg.MasterGroupCode, sg.ClientType, sg.BillingCoordinator, sg.Billing_Date, sg.[Name]
    SELECT
        {
            -- how to select the columns to return values like LINQ query
        }

We have an EF LINQ query with JOINs and GROUP BY and COUNT as below.

    var subgroupquery1 = from sg in _dbContext.SubGroups
              join e in _dbContext.Admins.Where(e => e.Status.StartsWith("Active") && e.SubGroupCode != indivsubgroupcode)
                on sg.SubGroupCode equals e.SubGroupCode
              join mf in _dbContext.MasterSetups.Where(e => groupstoinclude.Contains(e.AdminGroup)).Select(mfs => new { mfs.RefCode})
                on e.EntCode equals mf.RefCode
              group e by sg into g
              select new SubGroupInfo
              {
                  SubGroupKey = g.Key.SubGroupCode,
                  SubGroupCode = g.Key.SubGroupCode,
                  SubGroupName = string.Concat(g.Key.Name, " (", g.Key.SubGroupCode, ")"),
                  MasterGroupCode = g.Key.MasterGroupCode,
                  NoOfEntities = g.Count(),
                  Managers = g.Select(x => x.Manager).Distinct(),
                  Officers = g.Select(x => x.Officer).Distinct(),
                  ClientType = g.Key.ClientType,
                  Coordinator = g.Key.Coordinator,
                  GroupDate = g.Key.Group_Date
              };

The tables being queried have large number of columns.
EF is generating a complex SQL query and unnecessarily selecting all columns making it slow and memory heavy.

I am trying to convert this LINQ query to SQL query, something like below. But I am not able to select the columns after grouping. Can someone help to complete the query please?

    SELECT sg.SubGroupCode, sg.MasterGroupCode, sg.ClientType, sg.BillingCoordinator, sg.Billing_Date, sg.[Name]
    FROM vw_W360_SubGroup sg
    JOIN vw_W360_EntityAdmin e ON sg.SubGroupCode = e.SubGroupCode
    JOIN vw_W360_MasterFileSetup mf ON e.EntCode = mf.RefCode
    GROUP BY sg.SubGroupCode, sg.MasterGroupCode, sg.ClientType, sg.BillingCoordinator, sg.Billing_Date, sg.[Name]
    SELECT
        {
            -- how to select the columns to return values like LINQ query
        }

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

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

发布评论

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

评论(1

梦里梦着梦中梦 2025-02-05 16:42:09

当您进行选择查询时,您要选择和返回的所有列应仅在第一个选择语句中。

SELECT 
    sg.SubGroupCode, 
    sg.MasterGroupCode, 
    sg.ClientType, 
    sg.BillingCoordinator, 
    sg.Billing_Date, 
    sg.[Name],
    e.EntCode -- If you want to select columns from your joins, you should do it like this.
FROM vw_W360_SubGroup sg
/* <Your type of join> */ vw_W360_EntityAdmin e ON sg.SubGroupCode = e.SubGroupCode
/* <Your type of join> */ vw_W360_MasterFileSetup mf ON e.EntCode = mf.RefCode
GROUP BY sg.SubGroupCode, sg.MasterGroupCode, sg.ClientType, sg.BillingCoordinator, sg.Billing_Date, sg.[Name]

不同类型的SQL连接
以下是SQL中的连接的不同类型:(

  • 内)join:返回两个都具有匹配值的记录
    左表
  • (外部)加入:从左表返回所有记录,
    以及右表右表(外部)加入的匹配记录

  • 从右表返回所有记录,并匹配的记录
    从左表
  • 完整(外部)加入:在那里返回所有记录
    是左或右表中的匹配项,

您可以选择更适合您的场景的联接类型。

参考:
https://www.w3schools.com/sql/sql/sql_join.asp

When you are doing a SELECT query, all the columns that you want to select and return should be only on the your first SELECT statement.

SELECT 
    sg.SubGroupCode, 
    sg.MasterGroupCode, 
    sg.ClientType, 
    sg.BillingCoordinator, 
    sg.Billing_Date, 
    sg.[Name],
    e.EntCode -- If you want to select columns from your joins, you should do it like this.
FROM vw_W360_SubGroup sg
/* <Your type of join> */ vw_W360_EntityAdmin e ON sg.SubGroupCode = e.SubGroupCode
/* <Your type of join> */ vw_W360_MasterFileSetup mf ON e.EntCode = mf.RefCode
GROUP BY sg.SubGroupCode, sg.MasterGroupCode, sg.ClientType, sg.BillingCoordinator, sg.Billing_Date, sg.[Name]

Different Types of SQL JOINs
Here are the different types of the JOINs in SQL:

  • (INNER) JOIN: Returns records that have matching values in both
    tables
  • LEFT (OUTER) JOIN: Returns all records from the left table,
    and the matched records from the right table
  • RIGHT (OUTER) JOIN:
    Returns all records from the right table, and the matched records
    from the left table
  • FULL (OUTER) JOIN: Returns all records when there
    is a match in either left or right table

You can select the type of join more suitable for your scenario.

References:
https://www.w3schools.com/sql/sql_join.asp

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