Grouping by two values and calculating the third one
<p>I have a class that contains results of some benchmarking tests. The class looks more or less like this:</p>

<pre><code>public class TestResult
{
    public string TestName { get; set; }
    public int SampleSize { get; set; }
    public double Result { get; set; }
}
</code></pre>

<p>I have ran all tests many times, so I have many results for each sample size, like:</p>

<pre><code>TestName: "MemoryTest", SampleSize: 1024, Result: 2.4741
TestName: "MemoryTest", SampleSize: 1024, Result: 2.5029
TestName: "MemoryTest", SampleSize: 1024, Result: 2.4902
TestName: "MemoryTest", SampleSize: 2048, Result: 4.5654
TestName: "MemoryTest", SampleSize: 2048, Result: 4.7156
TestName: "MemoryTest", SampleSize: 2048, Result: 4.2238
TestName: "MemoryTest", SampleSize: 4096, Result: 10.1863
TestName: "MemoryTest", SampleSize: 4096, Result: 9.9471
TestName: "MemoryTest", SampleSize: 4096, Result: 10.2240

TestName: "GraphicsTest", SampleSize: 1024, Result: 2.4741
TestName: "GraphicsTest", SampleSize: 1024, Result: 2.5029
TestName: "GraphicsTest", SampleSize: 1024, Result: 2.4902
TestName: "GraphicsTest", SampleSize: 2048, Result: 4.5654
TestName: "GraphicsTest", SampleSize: 2048, Result: 4.7156
TestName: "GraphicsTest", SampleSize: 2048, Result: 4.2238
TestName: "GraphicsTest", SampleSize: 4096, Result: 10.1863
TestName: "GraphicsTest", SampleSize: 4096, Result: 9.9471
TestName: "GraphicsTest", SampleSize: 4096, Result: 10.2240
</code></pre>

<p>Now I would like to create a List containing the average of all the results, but grouped by both TestName and SampleSize. </p>

<p>Looking on an example above, I'd like to have a list containing 6 result : 3 for GraphicsTest and 3 for MemoryTest, with averages for each SampleSize. </p>

<p>I have trouble grouping by two different columns. I've tried it for a few ways, but I can't get a grasp on it... </p>

<p>My best approach so far is:</p>

<pre><code>        var _Consolidation =
            from _R in Results
            group _R by new { _R.TestName, _R.SampleSize } into _G
            select new TestResult()
            {
                TestName = _G.Key.TestName,
                SampleSize = _G.Key.SampleSize,
                Result = from _Res 
                         in Results
                         where _Res.TestName == _G.Key.TestName &amp;&amp; _Res.SampleSize == _G.Key.SampleSize
                         select _X =&gt; new
                         {
                             // ? Average Result? How to calculate it?
                         }
            };
</code></pre>

<p>But I'm not sure if I'm going the right way.</p>