Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select case in LINQ [duplicate]

how can I translate this into 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 

Thank you!

like image 864
Anders Avatar asked Nov 22 '10 09:11

Anders


1 Answers

Maybe this works:

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() }; 
like image 90
Botz3000 Avatar answered Sep 19 '22 11:09

Botz3000