生成linq查询以获取单列中不同记录的计数

时间:2015-09-14 08:48:40

标签: sql linq

我想生成类似于

的linq查询
select distinct HashTagName, count(HashTagName) as CountOf 
from tblUserHashTags group by HashTagName order by CountOf desc

请建议我这样做。

1 个答案:

答案 0 :(得分:1)

使用查询语法应该非常简单: -

var result = from uh in db.tblUserHashTags
             group uh by uh.HashTagName into g
             let count = g.Count()
             orderby count descending
             select new 
             {
                 HashTagName = g.Key,
                 CountOf = count
             };