日期:2014-05-20  浏览次数:20701 次

数据库语句和linq的转换
select Name, 

sum(case when Num between 0 and 10 then Num else 0 end) as Num1, 

sum(case when Num between 11 and 20 then Num else 0 end) as Num2, 

sum(case when Num between 21 and 30 then Num else 0 end) as Num3

from Table_1

group by Name
请问这段怎么用LINQ语句来表示

------解决方案--------------------
var query= from t in db.Table_1
group t by t.Name into g
select new {
Name=g.Key,
Num1=g.Sum(m=>(m.Num>0 && m.Num<10)?m.Num:0,
Num2=g.Sum(m=>(m.Num>11 && m.Num<20)?m.Num:0,
Num3=g.Sum(m=>(m.Num>21 && m.Num<30)?m.Num:0
};