Find average of collection of TimeSpans

Solution 1:

You can use the Average overload that takes a collection of long in parameter:

double doubleAverageTicks = sourceList.Average(timeSpan => timeSpan.Ticks);
long longAverageTicks = Convert.ToInt64(doubleAverageTicks);

return new TimeSpan(longAverageTicks);

Solution 2:

var average = new TimeSpan(sourceList.Select(ts => ts.Ticks).Average());

Note, your method returns a Nullable, but doesn't need to, unless you want to return null if the source list is empty, in which case just do a separate check first.

Solution 3:

In Addition to the above answer, I would suggest you take an average on the Seconds or MilliSeconds level (depending on what you require)

sourceList.Average(timeSpan => timeSpan.ToTalMilliseconds)

Now using this value you could arrive at the new TimeSpan using

TimeSpan avg = TimeSpan.FromMilliseconds(double value here)