Grouping by two values and calculating the third one

Viewed 47

I have a class that contains results of some benchmarking tests. The class looks more or less like this:

public class TestResult
{
    public string TestName { get; set; }
    public int SampleSize { get; set; }
    public double Result { get; set; }
}

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

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

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

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.

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

My best approach so far is:

        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 && _Res.SampleSize == _G.Key.SampleSize
                         select _X => new
                         {
                             // ? Average Result? How to calculate it?
                         }
            };

But I'm not sure if I'm going the right way.

2 Answers
Related