本文介绍了LINQ查询以查找时间跨度的最大平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组包含两点的数据; 瓦特"和一个时间戳.

I have a set of data that has two points; "watts" and a time stamp.

每个数据点之间相隔1秒.

Each data point is separated by 1 second.

所以看起来像这样:

0:01 100
0:02 110
0:03 133
0:04 280

.....

数据集长达几个小时.

The data set is a couple hours long.

我想写一个查询,在其中我可以找到不同时间段(5秒,1分钟,5分钟,20分钟等)的最大平均瓦数.

I'd like to write a query where I can find the maximum average watts for different time periods (5 seconds, 1 minutes, 5 minutes, 20 minutes, ect).

我还想知道最大平均值出现在数据集中的哪个位置.

I'd also like to know where in the data set that maximum average took place.

修改
我认为我需要使用移动平均线和适当的时段(例如10秒)进行查询.得到该结果后,我会查询该结果以找到最大值.

Edit
I think I need to do a query with a moving average and the appropriate bucket (let's say 10 seconds). Once I get that result, I query that to find the max.

推荐答案

好吧,一个工作的人帮助了我.这是LINQ Pad的答案.

Okay, a guy at work helped me. Here's the answer in LINQ Pad.

var period = 10;
var rnd = new Random();
// Create some data.
var series = Enumerable.Range(0, 3600)
    .Select(i => Tuple.Create(new TimeSpan(0, 0, i), rnd.Next(300))).ToList();

var item = Enumerable.Range(0, 3600).AsParallel() 
                .Select(i => series.Skip(i).Take(10)) 
                .Select((e, i) => new { Average = e.Sum(x => x.Item2) / e.Count(), Second = i }) 
                .OrderByDescending(a => a.Second).Dump();

                item.First().Dump(); 

这篇关于LINQ查询以查找时间跨度的最大平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 18:25