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

夙愿已清 提交于 2019-12-07 10:06:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!