I have a class named HardwarePerformance contain these fields: time,cpuUsagePercent,memoryUsagePercent,diskUsage,diskRead,diskWrite,latency.
I had fill a List object to contain the not statistics data.
I need to calculating the average value for all these fields in a Linq command.
I don’t know how to get the average value with Linq. Follow is the code I wrote (It’s incorrect of course).
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);
The syntax of this code is correct. But I want to calculating all the average value at the same time, how do I write my code?
You can use “Group By” then you can use an average projection to calculate.
LINQ Average