简体   繁体   English

如何在Dictionary集合中使用LINQ将TimeSpans添加到一起

[英]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? 是否可以将字典中的所有相等键组合在一起,并使用LINQ将相应的TimeSpans添加到一起?

Something like this (but this doesnt work) 像这样的东西(但这不起作用)

var p = myDictionaryStringTimeStamp.Sum(x => x.Value.Add(myTimeStamp);

You can do it with the Aggregate LINQ method like this: 您可以使用Aggregate LINQ方法执行此操作,如下所示:

var totalTime = myDictionaryStringTimeStamp
    .Values
    .Aggregate(new TimeSpan(0), (p, v) => p.Add(v));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM