<p>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.</p>

<p>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.</p>

<pre><code>╔════════╦═════════════╦════════╦══════╗
║ SiteId ║ VisitorType ║ Last30 ║ Total║
╠════════╬═════════════╬════════╬══════╣
║      1 ║           1 ║     10 ║  100 ║
║      1 ║           2 ║     40 ║  140 ║
║      2 ║           1 ║     20 ║  180 ║
╚════════╩═════════════╩════════╩══════╝
</code></pre>

<p>In SQL, I can easily get the counts for SiteID 1 with the following:</p>

<pre><code>SELECT SiteId,  
       SUM(Last30) AS Last30Sum  
FROM Sites  
WHERE SiteId = 1  
GROUP BY SiteId
</code></pre>

<p>and should get a row like...</p>

<pre><code>╔════════╦════════════╗
║ SiteId ║ Last30Total║
╠════════╬════════════╣
║      1 ║         50 ║
╚════════╩════════════╝
</code></pre>

<p>However I'm not sure how to get this result using Linq.  I've tried:</p>

<pre><code>var statsRecord = from ss in db.SiteStats  
    where ss.SiteId == siteId  
    group ss by ss.SiteId into ss  
    select ss;
</code></pre>

<p>but I'm not able to get back the total with something like <code>statsRecord.Last30</code></p>

<p>Can someone please let me know where I'm going wrong?  Any help is appreciated.</p>

[{'answer_id': 34951, 'author': 'Tom Mayfield', 'author_id': 2314, 'author_profile': 'https://Stackoverflow.com/users/2314', 'pm_score': 2, 'selected': False, 'text': '<p>Easiest way for me to illustrate is using in-memory objects so it\'s clear what\'s happening. LINQ to SQL should be able to take that same LINQ query and translate it into appropriate SQL.</p>\n\n<pre><code>public class Site\n{\n    static void Main()\n    {\n        List&lt;Site&gt; sites = new List&lt;Site&gt;()\n        {\n            new Site() { SiteID = 1, VisitorType = 1, Last30 = 10, Total = 100, },\n            new Site() { SiteID = 1, VisitorType = 2, Last30 = 40, Total = 140, },\n            new Site() { SiteID = 2, VisitorType = 1, Last30 = 20, Total = 180, },\n        };\n\n        var totals =\n            from s in sites\n            group s by s.SiteID into grouped\n            select new\n            {\n                SiteID = grouped.Key,\n                Last30Sum = \n                    (from value in grouped\n                     select value.Last30).Sum(),\n            };\n\n        foreach (var total in totals)\n        {\n            Console.WriteLine("Site: {0}, Last30Sum: {1}", total.SiteID, total.Last30Sum);\n        }\n    }\n\n    public int SiteID { get; set; }\n    public int VisitorType { get; set; }\n    public int Last30 { get; set; }\n    public int Total { get; set; }\n}\n</code></pre>\n'}, {'answer_id': 36678, 'author': 'Ben Laan', 'author_id': 2918, 'author_profile': 'https://Stackoverflow.com/users/2918', 'pm_score': 6, 'selected': True, 'text': '<p>Actually, although Thomas\' code will work, it is more succint to use a lambda expression:</p>\n\n<pre><code>var totals =\nfrom s in sites\ngroup s by s.SiteID into grouped\nselect new\n{\n    SiteID = grouped.Key,\n    Last30Sum = grouped.Sum( s =&gt; s.Last30 )\n};\n</code></pre>\n\n<p>which uses the Sum extension method without the need for a nested LINQ operation.</p>\n\n<p>as per the LINQ 101 examples - <a href="http://msdn.microsoft.com/en-us/vcsharp/aa336747.aspx#sumGrouped" rel="noreferrer">http://msdn.microsoft.com/en-us/vcsharp/aa336747.aspx#sumGrouped</a></p>\n'}]
['https://Stackoverflow.com/questions/34913', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2034/']