【发布时间】: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