0

I have a list of items like so:

enter image description here

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:

enter image description here

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()
});

enter image description here

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

Community
  • 1
  • 1
CallumVass
  • 11,288
  • 26
  • 84
  • 154

1 Answers1

1

Instead of getting the average for each array in the group, make a range that is the length of the arrays and get the average for the corresponding item from each array:

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 = Enumerable.Range(0, 3).Select(i => (int)Math.Round(x.Average(z => z.Y[i]))).ToArray()
});
Guffa
  • 687,336
  • 108
  • 737
  • 1,005