0

我有一个名为 HardwarePerformance 的类,其中包含以下字段:时间、cpuUsagePercent、memoryUsagePercent、diskUsage、diskRead、diskWrite、latency。我已经填充了一个 List 对象来包含非统计数据。我需要计算 Linq 命令中所有这些字段的平均值。我不知道如何使用 Linq 获得平均值。下面是我写的代码(当然是不正确的)。

var _hardwarePerf = (from _stats in _statsList 
                    where _time>=DateTime.Parse("2012-04-04 00:00:00")
                        &&_time<=DateTime.Parse("2012-04-04 11:00:00") 
                    select new {
                           Latency = _stats.latency, 
                           CpuUsagePercent = _stats.cpuUsagePercent, 
                           MemoryUsagePercent = _stats.memoryUsagePercent, 
                           DiskUsage = _stats.diskUsage, DiskRead = _stats.diskRead, 
                           DiskWrite = _stats.diskWrite
                    }).Average(Latency => Latency.Latency);

此代码的语法是正确的。但是我想同时计算所有的平均值,我该如何编写我的代码?

4

4 回答 4

1

LINQ 中没有为此的内置方法。如果您不介意您将多次迭代集合,并假设匿名对象对您来说没问题,您可以执行以下操作(其中filteredStats包含您想要平均的数据,可能按时间过滤):

var averages =
    new
    {
        Latency = filteredStats.Average(x => x.Latency),
        CpuUsagePercent = filteredStats.Average(x => x.CpuUsagePercent),
        …
    };

如果这不是您想要的,您可能必须自己计算平均值。

于 2012-04-19T07:22:57.127 回答
0

下面计算一次迭代中的所有平均值,尽管代码量很大。

请注意,我假设time它是您的 stats 对象上的一个属性,否则您select where time没有多大意义。

var _hardwareAccumulator =
    _statsList
        .Where(s =>
            s.time >= DateTime.Parse("2012-04-04 00:00:00") &&
            s.time <= DateTime.Parse("2012-04-04 11:00:00"))
        .Aggregate(
            new
            {
                count = 0,
                cpuUsagePercent = 0.0,
                memoryUsagePercent = 0.0,
                diskUsage = 0.0,
                diskRead = 0.0
            },
            (acc, s) =>
                new
                {
                    count = acc.count + 1,
                    cpuUsagePercent =
                        acc.cpuUsagePercent + s.cpuUsagePercent,
                    memoryUsagePercent =
                        acc.memoryUsagePercent + s.memoryUsagePercent,
                    diskUsage =
                        acc.diskUsage + s.diskUsage,
                    diskRead =
                        acc.diskRead + s.diskRead
                }
            );
var _hardwarePerf = new
{
    cpuUsagePercent =
        _hardwareAccumulator.cpuUsagePercent / _hardwareAccumulator.count,
    memoryUsagePercent =
        _hardwareAccumulator.memoryUsagePercent / _hardwareAccumulator.count,
    diskUsage =
        _hardwareAccumulator.diskUsage / _hardwareAccumulator.count,
    diskRead =
        _hardwareAccumulator.diskRead / _hardwareAccumulator.count
};
于 2012-04-19T07:32:56.450 回答
0

您要么需要在单独的语句中执行此操作,要么可以以这种不太直观的方式执行此操作:

var averages = statsList.Where(s => s.time >= new DateTime(2012, 4, 4) && s.time <= new DateTime(2012, 4, 04, 11, 0, 0))
            .GroupBy(s => 1)
            .Select(grp => new
            {
                Latency = grp.Average(s => s.latency),
                CpuUsagePercent = grp.Average(s => s.cpuUsagePercent),
                MemoryUsagePercent = grp.Average(s => s.memoryUsagePercent),
                DiskUsage = grp.Average(s => s.diskUsage),
                DiskRead = grp.Average(s => s.diskRead),
                DiskWrite = grp.Average(s => s.diskWrite)
            }).FirstOrDefault();

请注意,.GroupBy(s => 1)只是为了能够计算每个属性的平均值。另请注意,我已从您的属性名称中删除了下划线,并为您的课程添加了时间

另一种方法更容易阅读:

var theStats = statsList.Where(s => s.time >= new DateTime(2012, 4, 4) && s.time <= new DateTime(2012, 4, 04, 11, 0, 0));
var Latency = theStats.Average(s => s.latency);
var CpuUsagePercent = theStats.Average(s => s.cpuUsagePercent);
// ....
于 2012-04-19T07:33:30.923 回答
-2

您可以使用“分组依据”,然后您可以使用平均投影来计算。

var query = from _stats in _statsList 
    group _stats by new {
               Latency = _stats.latency, 
               CpuUsagePercent = _stats.cpuUsagePercent, 
               MemoryUsagePercent = _stats.memoryUsagePercent, 
               DiskUsage = _stats.diskUsage, 
               DiskRead = _stats.diskRead, 
               DiskWrite = _stats.diskWrite

    } into myGroup
    select new {
        AverageVal = myGroup.Average(x => x.Value),
        CpuUsagePercent = myGroup.Key.CpuUsagePercent,
        ... // the rest of your projection
};

LINQ 平均值

于 2012-04-19T07:22:17.663 回答