简体   繁体   中英

How can I convert “SELECT col, COUNT(*)” to a Linq expression?

I have this query

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

How can I convert this to Linq?

Something like:

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() };

(Where id is the ID you're looking for - as a GUID, or a string, or whatever the right type is.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM