I have a rather specialized query I am trying to figure out in C#.
I have a class:
class TimeValues
{
DateTime When;
ImmutableArray<float> Values;
}
This represents a report of a number of sensors at a particular time. Which I use in an ImmutableArray<TimeValues> SomeArray
, that represents a series of reports often down to the second.
The problem I am trying to solve is how to group by 30 second intervals, and average the reports of each sensor individually.
So for example, if I have two reports:
s1 s2 s3
1:20 10 20 30
1:21 30 50 70
and we assume that t1 and t2 are within 30 seconds of each other, I want the operation to result in:
s1 s2 s3
1:00 avg(10,30) avg(20,50) avg(30,70)
I have started with something such as:
SomeArray.GroupBy(k => k.When.Second >= 30
? k.When.AddSeconds(-k.When.Second + 30)
: k.When.AddSeconds(-k.When.Second), k => k.Values)
.Select(group => new TimeValues(group.Key, ...))
It is the last line that I can't quite figure out. One point that must be stressed is that the order of the values being averaged must be maintained as it has to correspond with the sensors reporting. This is my first time using group by in LINQ, and probably one of the more complicated.