Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum TimeSpan array with a one-liner?

Is there a way to aggregate multiple aggregates to 1 time span?

Dim times = { 
  New TimeSpan(1, 0, 0),
  New TimeSpan(1, 10, 0),
  New TimeSpan(1, 50, 0),
  New TimeSpan(0, 20, 0),
  New TimeSpan(0, 10, 0)
}

Dim sum As New TimeSpan
For Each ts In times
  sum = sum.Add(ts)
Next

'That's what I desire:
sum = times.Sum
sum = times.Aggregate

I am looking for some built in capability I don't know about.

Update Please read my comment on Reed Copsey's answer.

like image 448
Shimmy Weitzhandler Avatar asked May 03 '26 00:05

Shimmy Weitzhandler


1 Answers

C#:

TimeSpan sum = times.Aggregate((t1, t2) => t1.Add(t2));

VB.NET:

Dim sum As TimeSpan = times.Aggregate(Function(t1, t2) t1.Add(t2))
like image 199
Yuriy Faktorovich Avatar answered May 05 '26 21:05

Yuriy Faktorovich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!