如何将“SELECT col,COUNT(*)”转换为Linq表达式?

时间:2011-07-12 17:55:59

标签: c# linq entity-framework

我有这个查询

SELECT Reaction, COUNT(*) as 'Votes' FROM Member_Reaction mr
    WHERE mr.[Entitiy ID] = '259F16A0-9635-4F58-B645-0AEBAAC09D46'
    GROUP BY Reaction

如何将其转换为Linq?

1 个答案:

答案 0 :(得分:7)

类似的东西:

var query = from item in db.MemberReactions
            where item.ID == id
            group item by item.Reaction into g
            select new { Reaction = g.Key, Count = g.Count() };

(其中id是您要查找的ID - 作为GUID,字符串或任何正确的类型。)