将sql查询转换为lambda表达式

时间:2014-06-13 04:15:16

标签: c# sql .net linq lambda

如何将以下sql查询转换为lambda表达式?

SELECT Username, COUNT(Username) AS Count 
FROM user 
GROUP BY Username

2 个答案:

答案 0 :(得分:0)

 var UserCount = yourEntityObject.GroupBy(m => m.Username)
                       .Select(c => new {Username = c.Key, Count = c.Count()});

答案 1 :(得分:0)

尝试以下方法:

var grpUsers = empList.GroupBy(user => user.Username)
                         .Select(lst => new {Username = lst.Key, Count = lst.Count()} );