使用linq编写sql语句

时间:2015-02-18 10:14:02

标签: mysql linq

如何在LINQ中编写以下sql代码

select count(1) Cnt,state 
from yourtable
group by state
order by Cnt desc Limit 5

我正在尝试这样的事情

(from topPro in CS.state group topPro by new {
       Name = topPro.state.Name
} into g
select new{
     StateName = g.Key.Name,
     Count = g.Count(x=>x.state)
}

1 个答案:

答案 0 :(得分:1)

我得到了答案

var top5Pros = (from topPro in CS.state
group topPro by new { Name = topPro.state.Name } into g
select new
{
StateName = g.Key.Name,
Count = g.Count()
}).OrderByDescending(w => w.Count).Take(5);