Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean way to reduce many TimeSpans into fewer, average TimeSpans?

I have a C# Queue<TimeSpan> containing 500 elements.

I need to reduce those into 50 elements by taking groups of 10 TimeSpans and selecting their average.

Is there a clean way to do this? I'm thinking LINQ will help, but I can't figure out a clean way. Any ideas?

like image 683
Judah Gabriel Himango Avatar asked Dec 30 '22 03:12

Judah Gabriel Himango


1 Answers

I would use the Chunk function and a loop.

foreach(var set in source.ToList().Chunk(10)){
    target.Enqueue(TimeSpan.FromMilliseconds(
                            set.Average(t => t.TotalMilliseconds)));
}

Chunk is part of my standard helper library. http://clrextensions.codeplex.com/

Source for Chunk

like image 97
Jonathan Allen Avatar answered Jan 13 '23 12:01

Jonathan Allen