小编典典

在LINQ中选择大小写

sql

如何将其转换为LINQ?

select t.age as AgeRange, count(*) as Users
from (
  select case  
    when age between 0 and 9 then ' 0-25'
    when age between 10 and 14 then '26-40'
    when age between 20 and 49 then '60-100'
    else '50+' end as age
  from user) t
group by t.age

谢谢!


阅读 177

收藏
2021-04-15

共1个答案

小编典典

也许这可行:

from u in users
let range = (u.Age >= 0  && u.Age < 10 ? "0-25" :
             u.Age >= 10 && u.Age < 15 ? "26-40" :
             u.Age >= 15 && u.Age < 50 ? "60-100" :
            "50+")
group u by range into g
select new { g.Key, Count=g.Count() };
2021-04-15