C#中的时间跨度总和 [英] Sum of TimeSpans in C#
问题描述
我有一个包含 TimeSpan 变量的对象集合:
MyObject{时间跨度持续时间{得到;放;}}
我想用 LINQ 来总结这些时间.当然,(从 MyCollection 中的 r 选择 r.TheDuration).Sum();不起作用!
我正在考虑将 TheDuration 的数据类型更改为 int,然后将其求和并将总和转换为 TimeSpan.这会很混乱,因为我收藏中的每个 TheDuration 都被用作其他地方的时间跨度.
对这个总结有什么建议吗?
不幸的是,没有接受 IEnumerable
的 Sum
重载.此外,目前还没有为类型参数指定基于运算符的泛型约束的方法,因此即使 TimeSpan
是本机"可求和的,但泛型代码无法轻松获取这一事实.>
一种选择是,如您所说,总结一个等效于时间跨度的整数类型,然后再次将 那个 和转换为 TimeSpan
.对此的理想属性是 TimeSpan.Ticks
,准确地往返.但是根本没有必要更改类的属性类型;你可以项目:
var totalSpan = new TimeSpan(myCollection.Sum(r => r.TheDuration.Ticks));
或者,如果您想坚持使用 TimeSpan 的 +
运算符进行求和,则可以使用 Aggregate
运算符:
var totalSpan = myCollection.Aggregate(时间跨度.零,(sumSoFar, nextMyObject) =>sumSoFar + nextMyObject.TheDuration);
I have a collection of objects that include a TimeSpan variable:
MyObject
{
TimeSpan TheDuration { get; set; }
}
I want to use LINQ to sum those times. Of course, (from r in MyCollection select r.TheDuration).Sum(); doesn't work!
I'm thinking of changing the datatype of TheDuration to an int and then summing it and converting the sum to a TimeSpan. That will be messy because each TheDuration in my collection is used in as a timespan somewhere else.
Any suggestion on this summation?
Unfortunately, there isn't a an overload of Sum
that accepts an IEnumerable<TimeSpan>
. Additionally, there's no current way of specifying operator-based generic constraints for type-parameters, so even though TimeSpan
is "natively" summable, that fact can't be picked up easily by generic code.
One option would be to, as you say, sum up an integral-type equivalent to the timespan instead, and then turn that sum into a TimeSpan
again. The ideal property for this is TimeSpan.Ticks
, which round-trips accurately. But it's not necessary to change the property-type on your class at all; you can just project:
var totalSpan = new TimeSpan(myCollection.Sum(r => r.TheDuration.Ticks));
Alternatively, if you want to stick to the TimeSpan's +
operator to do the summing, you can use the Aggregate
operator:
var totalSpan = myCollection.Aggregate
(TimeSpan.Zero,
(sumSoFar, nextMyObject) => sumSoFar + nextMyObject.TheDuration);
这篇关于C#中的时间跨度总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!