【问题标题】:SQL Query to Linq to Entities - C#SQL 查询到 Linq 到实体 - C#
【发布时间】:2016-06-07 20:43:20
【问题描述】:

我一直在尝试将此 SQL 语句转换为 linq,因为我正在尝试将功能移动到程序中。

这是SQL语句

SELECT cust.sg_group_name                     AS customer, 
   (SELECT Sum(du.used_space) 
    FROM   sg_groups AS clnt 
           LEFT JOIN client_disk_usage AS du 
                  ON clnt.sg_group_id = du.sg_group_id 
                     AND clnt.group_role_id = 3 
    WHERE  clnt.parent_group_id = cust.sg_group_id 
           AND du.day_of_month = 15 
           AND du.month_of_year = 05 
           AND du.used_space_year = 2016) AS disk_usage 
FROM   sg_groups AS cust 
WHERE  cust.group_role_id = 2 
ORDER  BY cust.sg_group_name 

基本上输出只是一个包含两列的列表

customer      disk_usage
Customer1    136401537652 
Customer2    42208008210 

如果可能的话,我只想将其转换为 linq 语句。我曾尝试将查询放入 LinqPad,但它似乎不想从 SQL 转换为 Linq(只是出现一个空白的白页)。我自己对查询有所了解,但我得到的东西要么完全不起作用,要么结果数量不正确。

如果有人有任何建议,那就太好了!

【问题讨论】:

标签: c# sql linq-to-entities


【解决方案1】:

disk_usage(Sub Query) 有点复杂。在这里转换。试试这个

var CoreList = (from clnt in EntityName.sg_groups 
                    join du in EntityName.client_disk_usage 
                    on new { GrpId = clnt.sg_group_id, RoleId = clnt.group_role_id } equals new { GrpId = du.sg_group_id, RoleId = 3 } into LJ
                    from RT in LJ.DefaultIfEmpty()
                    where du.day_of_month == 15 && du.month_of_year == 05 && du.used_space_year == 2016
                    select new {clnt, du, RT}
                   ).ToList();

    var CoreListSet = CoreList.Select(i=> new YourEntityClass
                      {
                      //Fetch the ParentGroupId & UsedSpace
                      }).ToList();

    var CoreListComplete = (from cl in CoreListSet 
                           join cust in EntityName.sg_groups 
                           on cust.sg_group_id equals cl.parent_group_id).ToList();

现在获取 CoreListComplete 的总和,只需在 Linq 中实现基本的 Select Query!

【讨论】:

    【解决方案2】:

    对延迟回复表示歉意。我已将@Anil 标记为答案,因为这是帮助我找到答案的答案。您的解决方案确实有效@Sathish,但它可以在一个命令中完成。这是我的最终解决方案。非常感谢您的帮助!

    storeGridUsage = (
                    from cust in db.sg_groups
                    from client in db.sg_groups
                    join du in db.client_disk_usage on client.SG_GROUP_ID equals du.SG_GROUP_ID
                    where client.GROUP_ROLE_ID == 3
                    where client.PARENT_GROUP_ID == cust.SG_GROUP_ID && du.DAY_OF_MONTH == day && du.MONTH_OF_YEAR == month && du.USED_SPACE_YEAR == year
                    where cust.GROUP_ROLE_ID == 2
                    orderby cust.SG_GROUP_NAME
                    group new {cust, du} by cust.SG_GROUP_NAME
                    into g
                    select new StoreGridUsage
                    {
                        CustomerName = g.Key,
                        DiskUsageInBytes = g.Sum(o => o.du.USED_SPACE)
                    }).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多