Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ到SQL:组、计数、求和。I';我很困惑_C#_Sql_Sql Server_Linq - Fatal编程技术网

C# LINQ到SQL:组、计数、求和。I';我很困惑

C# LINQ到SQL:组、计数、求和。I';我很困惑,c#,sql,sql-server,linq,C#,Sql,Sql Server,Linq,大家早上好, 我整个上午都在做这件事,感觉自己撞到了墙。我希望在这一点上能得到任何建议。 我的表格基本如下: 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

大家早上好, 我整个上午都在做这件事,感觉自己撞到了墙。我希望在这一点上能得到任何建议。 我的表格基本如下:

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到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

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


这样的方法应该可以工作,只需运行一个查询

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() 
            }));

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

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

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() }
);

忽略命名约定。我更改了名称以不反映任何信息。
Groupby(x=>x.LivingSpace,(x,y)=>new{Unit=x,Count=y.Count()})previousQuery.Sum(x=>x.Count)它仍将对db执行两个查询。@vittore,而不是根据SQL探查器。它显示一个带有
UNION ALL
的单个查询。我是针对我已经有的一个表来做的,但是如果你想看的话,我很乐意发布结果查询。实际上这很有趣,仔细想想,你传递给Union的子查询返回iQuery,所以它应该是可延迟执行的。谢谢你,这很有效。非常感谢你的帮助。它快把我逼疯了!
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
                    }));
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() 
            }));
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
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() }
);