I have a list of items like so:
Where each element in the list follows the same structure. Each Y
property is an int[]
which has 3 values.
I am then doing a GroupBy
on this list to split the data up into 60 second periods:
var newChart = chart.GroupBy(x => x.DateAdded.RoundUp(TimeSpan.FromSeconds(60)));
RoundUp
is an extension method I got from here: How can I round up the time to the nearest X minutes?
This creates a list like so, where the values are Grouped By the minute, which is what I'd expect:
So for example for the minute 20:18
there were 5 entries, this value may change, sometimes it may be 6 entries, sometimes 1 entry and so forth.
So my question is, how can I cast this new Grouped By list into the type of my original List where the Y
values has the average of those entries in the group by. I've tried the following but the Y
values contain more than 3 entries:
var newChart = chart.GroupBy(x => x.DateAdded.RoundUp(TimeSpan.FromSeconds(60)))
.Select(x => new ChartPoint
{
DateAdded = x.Key,
X = x.Key.ToString(@"HH:mm:ss"),
Y = x.Select(y => (int)Math.Round(y.Y.Average(z => z), 0, MidpointRounding.AwayFromZero)).ToArray()
});
So for example, if the entry at 20:18
had only 2 entries, and the Y values for entry 1 was: 3, 4, 5
and the values for entry 2 were: 5, 10, 11
. Then I would expect the entry at 20:18
for Y
in this new list to be as follows: 4, 7, 8
because: (3+5)/2 = 4
, (4+10)/2 = 7
, (5+11)/2 = 8