I’m experimenting with Linq and am having trouble figuring out grouping. I’ve gone through several tutorials but for some reason can’t figure this out.
As an example, say I have a table (SiteStats) with multiple website IDs that stores a count of how many visitors by type have accessed each site in total and for the past 30 days.
╔════════╦═════════════╦════════╦══════╗ ║ SiteId ║ VisitorType ║ Last30 ║ Total║ ╠════════╬═════════════╬════════╬══════╣ ║ 1 ║ 1 ║ 10 ║ 100 ║ ║ 1 ║ 2 ║ 40 ║ 140 ║ ║ 2 ║ 1 ║ 20 ║ 180 ║ ╚════════╩═════════════╩════════╩══════╝
In SQL, I can easily get the counts for SiteID 1 with the following:
SELECT SiteId, SUM(Last30) AS Last30Sum FROM Sites WHERE SiteId = 1 GROUP BY SiteId
and should get a row like…
╔════════╦════════════╗ ║ SiteId ║ Last30Total║ ╠════════╬════════════╣ ║ 1 ║ 50 ║ ╚════════╩════════════╝
However I’m not sure how to get this result using Linq. I’ve tried:
var statsRecord = from ss in db.SiteStats where ss.SiteId == siteId group ss by ss.SiteId into ss select ss;
but I’m not able to get back the total with something like statsRecord.Last30
Can someone please let me know where I’m going wrong? Any help is appreciated.
Actually, although Thomas’ code will work, it is more succint to use a lambda expression:
which uses the Sum extension method without the need for a nested LINQ operation.
as per the LINQ 101 examples – http://msdn.microsoft.com/en-us/vcsharp/aa336747.aspx#sumGrouped