Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add TimeSpans together using LINQ when in a Dictionary collection

Is it possible to group up all equal keys in a Dictionary and add their corresponding TimeSpans together using LINQ?

Something like this (but this doesnt work)

var p = myDictionaryStringTimeStamp.Sum(x => x.Value.Add(myTimeStamp);
like image 980
dirtyw0lf Avatar asked Mar 23 '23 15:03

dirtyw0lf


1 Answers

You can do it with the Aggregate LINQ method like this:

var totalTime = myDictionaryStringTimeStamp
    .Values
    .Aggregate(new TimeSpan(0), (p, v) => p.Add(v));
like image 170
Sergey Kalinichenko Avatar answered Apr 05 '23 22:04

Sergey Kalinichenko