问题
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);
回答1:
You can do it with the Aggregate LINQ method like this:
var totalTime = myDictionaryStringTimeStamp
.Values
.Aggregate(new TimeSpan(0), (p, v) => p.Add(v));
来源:https://stackoverflow.com/questions/18455756/how-to-add-timespans-together-using-linq-when-in-a-dictionary-collection