我有一个包含一些基准测试结果的类。这堂课或多或少看起来像这样:
public class TestResult
{
public string TestName { get; set; }
public int SampleSize { get; set; }
public double Result { get; set; }
}
我已多次运行所有测试,因此每个样本大小都有很多结果,例如:
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
现在我想创建一个List,其中包含所有结果的平均值,但是按TestName和SampleSize分组。
查看上面的示例,我想要一个包含6个结果的列表:3个用于GraphicsTest,3个用于MemoryTest,每个SampleSize的平均值。
我无法通过两个不同的列进行分组。我已经尝试了几种方法,但我无法掌握它......
到目前为止,我最好的方法是:
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?
}
};
但我不确定我是否走得正确。
答案 0 :(得分:2)
您可以使用结果选择器malloc
使用this overload:
GroupBy
在var result = tests.GroupBy(t => new { t.TestName, t.SampleSize },
(k, v) => new
{
SampleSize = k.SampleSize,
TestName = k.TestName,
Average = v.Average(t => t.Result)
});
(k, v)
中是您分组的密钥,k
是每个组中的所有元素。
答案 1 :(得分:1)
你很接近,你需要按照你所拥有的匿名类型进行分组,然后对分组进行平均只是对该组集合的平均值:
event.which
理解分组的关键是一个组本质上是一个键/值集合,其中键是您所分组的,而值是符合组选择器的所有项的集合;这个集合是你可以为每个小组工作的。