将年龄范围 SQL 转换为 Linq 查询
Convert Age Range SQL to Linq Query
任何人都可以帮助我将此 SQL 查询转换为 Linq 查询。我尝试了不同的方法。但我还是做不到。
SELECT ISNULL(SUM(CASE WHEN Age < 10 THEN 1 ELSE 0 END), 0) AS Between0to10,
ISNULL(SUM(CASE WHEN Age BETWEEN 11 AND 20 THEN 1 ELSE 0 END), 0) AS Between11to20,
ISNULL(SUM(CASE WHEN Age BETWEEN 21 AND 30 THEN 1 ELSE 0 END), 0) AS Between21to30,
ISNULL(SUM(CASE WHEN Age BETWEEN 31 AND 40 THEN 1 ELSE 0 END), 0) AS Between31to40,
ISNULL(SUM(CASE WHEN Age BETWEEN 41 AND 50 THEN 1 ELSE 0 END), 0) AS Between41to50,
ISNULL(SUM(CASE WHEN Age BETWEEN 51 AND 60 THEN 1 ELSE 0 END), 0) AS Between51to60,
ISNULL(SUM(CASE WHEN Age BETWEEN 61 AND 70 THEN 1 ELSE 0 END), 0) AS Between61to70,
ISNULL(SUM(CASE WHEN Age BETWEEN 71 AND 80 THEN 1 ELSE 0 END), 0) AS Between71to80,
ISNULL(SUM(CASE WHEN Age BETWEEN 81 AND 90 THEN 1 ELSE 0 END), 0) AS Between81to90,
ISNULL(SUM(CASE WHEN Age BETWEEN 91 AND 100 THEN 1 ELSE 0 END), 0) AS Between91to100
FROM CovidMaster
试试这个查询
var result=(from item in data
group item by true into groupData
select new
{
Between0to10=groupData.Where(x=>x.Age >= 0 && x.Age <= 10).Count(),
Between11to20=groupData.Where(x=>x.Age >= 11 && x.Age <= 20).Count(),
Between21to30=groupData.Where(x=>x.Age >= 21 && x.Age <= 30).Count(),
Between31to40=groupData.Where(x=>x.Age >= 31 && x.Age <= 40).Count(),
Between41to50=groupData.Where(x=>x.Age >= 41 && x.Age <= 50).Count(),
Between51to60=groupData.Where(x=>x.Age >= 51 && x.Age <= 60).Count(),
Between61to70=groupData.Where(x=>x.Age >= 61 && x.Age <= 70).Count(),
Between71to80=groupData.Where(x=>x.Age >= 71 && x.Age <= 80).Count(),
Between81to90=groupData.Where(x=>x.Age >= 81 && x.Age <= 90).Count(),
Between91to100=groupData.Where(x=>x.Age >= 91 && x.Age <= 100).Count(),
}).ToList();
有关详细信息,请参阅此 link https://dotnetfiddle.net/u0uUyH
任何人都可以帮助我将此 SQL 查询转换为 Linq 查询。我尝试了不同的方法。但我还是做不到。
SELECT ISNULL(SUM(CASE WHEN Age < 10 THEN 1 ELSE 0 END), 0) AS Between0to10,
ISNULL(SUM(CASE WHEN Age BETWEEN 11 AND 20 THEN 1 ELSE 0 END), 0) AS Between11to20,
ISNULL(SUM(CASE WHEN Age BETWEEN 21 AND 30 THEN 1 ELSE 0 END), 0) AS Between21to30,
ISNULL(SUM(CASE WHEN Age BETWEEN 31 AND 40 THEN 1 ELSE 0 END), 0) AS Between31to40,
ISNULL(SUM(CASE WHEN Age BETWEEN 41 AND 50 THEN 1 ELSE 0 END), 0) AS Between41to50,
ISNULL(SUM(CASE WHEN Age BETWEEN 51 AND 60 THEN 1 ELSE 0 END), 0) AS Between51to60,
ISNULL(SUM(CASE WHEN Age BETWEEN 61 AND 70 THEN 1 ELSE 0 END), 0) AS Between61to70,
ISNULL(SUM(CASE WHEN Age BETWEEN 71 AND 80 THEN 1 ELSE 0 END), 0) AS Between71to80,
ISNULL(SUM(CASE WHEN Age BETWEEN 81 AND 90 THEN 1 ELSE 0 END), 0) AS Between81to90,
ISNULL(SUM(CASE WHEN Age BETWEEN 91 AND 100 THEN 1 ELSE 0 END), 0) AS Between91to100
FROM CovidMaster
试试这个查询
var result=(from item in data
group item by true into groupData
select new
{
Between0to10=groupData.Where(x=>x.Age >= 0 && x.Age <= 10).Count(),
Between11to20=groupData.Where(x=>x.Age >= 11 && x.Age <= 20).Count(),
Between21to30=groupData.Where(x=>x.Age >= 21 && x.Age <= 30).Count(),
Between31to40=groupData.Where(x=>x.Age >= 31 && x.Age <= 40).Count(),
Between41to50=groupData.Where(x=>x.Age >= 41 && x.Age <= 50).Count(),
Between51to60=groupData.Where(x=>x.Age >= 51 && x.Age <= 60).Count(),
Between61to70=groupData.Where(x=>x.Age >= 61 && x.Age <= 70).Count(),
Between71to80=groupData.Where(x=>x.Age >= 71 && x.Age <= 80).Count(),
Between81to90=groupData.Where(x=>x.Age >= 81 && x.Age <= 90).Count(),
Between91to100=groupData.Where(x=>x.Age >= 91 && x.Age <= 100).Count(),
}).ToList();
有关详细信息,请参阅此 link https://dotnetfiddle.net/u0uUyH