【问题标题】:LINQ to SQL: Group, Count, Sum. I'm so confusedLINQ to SQL:组、计数、总和。我很混乱
【发布时间】:2015-11-06 15:36:56
【问题描述】:

大家早上好, 我整个早上都被困在这上面,感觉就像撞到了墙上。我很乐意在这一点上给出任何建议。 我的表基本如下:

PatientName|LivingSpace
-----------|-----------
Patient 1  | Unit 1
Patient 2  | Unit 1
Patient 3  | Unit 2
Patient 4  | Unit 2
Patient 5  | Unit 3
Patient 6  | Unit 3
Patient 7  | Unit 3
Patient 8  | Unit 3

我需要一个 LINQ to SQL 查询来说明这一点:

   Unit|Count
   ----|-----
Unit 1 |  2
Unit 2 |  2
Unit 3 |  4
TOTAL  |  8

我的 SQL 查询工作正常,只是在将其转换为 LINQ 时遇到问题:

SELECT LivingSpace, COUNT(LivingSpace) AS LivingSpace
FROM PatientTable
WHERE Status = 'Active'
GROUP BY LivingSpace
    UNION ALL
    SELECT 'SUM' LivingSpace, COUNT(LivingSpace)
    FROM PatientTable

【问题讨论】:

  • 忽略命名约定。我更改了名称以不反映任何信息。
  • Groupby(x=>x.LivingSpace,(x,y)=>new{ Unit = x , Count = y.Count()}); 并得到总数你可以做previousQuery.Sum(x=>x.Count);

标签: c# sql sql-server linq


【解决方案1】:
var counts = from x in ctx.PatientTable
      group x by x.LivingSpace into y
      select new { Key = y.Key Count = y.Count() };

var total  = new { Key = "Total" , Count = ctx.PatientTable.Count() };

var full = counts.ToList();

full.Add(total);

【讨论】:

  • 这很好用。非常感谢你的帮助。这让我发疯了!
【解决方案2】:

如果您想在一个查询中完成所有操作,以下应该可以工作(当然要根据您的属性的实际名称进行调整)。

context.PatientTable.GroupBy(a => a.LivingSpace.Name, a => 1)
    .Select(a => new 
        {
            a.Key, 
            Total = a.Sum(q => q) 
        })
    .Union(PatientTable.Select(a => new 
        { 
            Key = "Total", 
            Total = PatientTable.Count() 
        }))

【讨论】:

  • 它仍然会对 db 进行两次查询。
  • @vittore,不是根据 SQL Profiler。它显示了一个带有UNION ALL 的查询。我是针对已有的表进行的,但如果您想查看结果查询,我很乐意发布。
  • 其实这很有意思,想了想,你传给Union的子查询返回了IQueriable,所以应该懒惰地执行。谢谢你
【解决方案3】:
var report = patients
                .GroupBy(p => p.LivingSpace)
                    .Select(g => new
                    {
                        Unit = g.Key,
                        Count = g.Count()
                    })
                .Union(patients
                    .Select(p => new
                    {
                        Unit = "Total",
                        Count = patients.Count
                    }));

【讨论】:

    【解决方案4】:

    这样的事情应该可以工作并且只运行一个查询。

    var results = db.PatientTable
        .GroupBy(p => p.LivingSpace)
        .Select(grp => new 
            { 
                Unit = grp.Key, 
                Count = grp.Count() 
            })         
        .Union(db.PatientTable
            .GroupBy(p => 1)
            .Select(grp => new 
                { 
                    Unit = "Total", 
                    Count = grp.Count() 
                }));
    

    【讨论】:

      【解决方案5】:

      我知道你得到了答案,但出于学习目的,这里是并排转换。

      您的 SQL(添加了一些别名以便更好地比较)

      SELECT P.LivingSpace, COUNT(P.*) AS Count
      FROM PatientTable AS P
      WHERE P.Status = 'Active'
      GROUP BY P.LivingSpace
      
      UNION ALL
      
      SELECT 'SUM' AS LivingSpace, COUNT(P.*) AS Count
      FROM PatientTable AS P
      

      LINQ 中的同一个查询

      var query =
      (
      from p in db.PatientTable
      where p.Status = "Active"
      group p by p.LivingSpace into g
      select new { LivingSpace = g.Key, Count = g.Count() }
      )
      .Concat
      (
      from p in db.PatientTable
      group p by "SUM" into g
      select new { LivingSpace = g.Key, Count = g.Count() }
      );
      

      【讨论】:

        猜你喜欢
        • 2013-07-19
        • 2011-07-18
        • 1970-01-01
        • 2015-05-29
        • 2010-12-22
        • 2010-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多