当前位置: 首页 > 工具软件 > Casein > 使用案例 >

Select case in LINQ

颜德馨
2023-12-01
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!


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

check this may help you
var query = from grade in sc.StudentGrade
                        join student in sc.Person on grade.Person.PersonID
                                      equals student.PersonID
                        select new
                        {
                            FirstName = student.FirstName,
                            LastName = student.LastName,
                            Grade = grade.Grade.Value >= 4 ? "A" :
                                        grade.Grade.Value >= 3 ? "B" :
                                        grade.Grade.Value >= 2 ? "C" :
                                        grade.Grade.Value != null ? "D" : "-"
                        }; 

the last maybe right!
Something like this?
var users = (from u in Users
             select new
             {
                User = u,
                AgeRange =
                    u.Age >= 0 && u.Age <= 9 ? "0-25"  :
                    u.Age <= 14              ? "26-50" :
                    u.Age <= 49              ? "60-100":
                                               "50+"
              }).GroupBy(e => e.AgeRange);


 类似资料:

相关阅读

相关文章

相关问答