用单行求和 TimeSpan 数组?

发布于 2024-09-09 11:46:58 字数 506 浏览 10 评论 0原文

有没有办法将多个聚合聚合到 1 个时间跨度?

Dim times = { 
  New TimeSpan(1, 0, 0),
  New TimeSpan(1, 10, 0),
  New TimeSpan(1, 50, 0),
  New TimeSpan(0, 20, 0),
  New TimeSpan(0, 10, 0)
}

Dim sum As New TimeSpan
For Each ts In times
  sum = sum.Add(ts)
Next

'That's what I desire:
sum = times.Sum
sum = times.Aggregate

我正在寻找一些我不知道的内置功能。

更新 请阅读我对Reed Copsey 的回答的评论。

Is there a way to aggregate multiple aggregates to 1 time span?

Dim times = { 
  New TimeSpan(1, 0, 0),
  New TimeSpan(1, 10, 0),
  New TimeSpan(1, 50, 0),
  New TimeSpan(0, 20, 0),
  New TimeSpan(0, 10, 0)
}

Dim sum As New TimeSpan
For Each ts In times
  sum = sum.Add(ts)
Next

'That's what I desire:
sum = times.Sum
sum = times.Aggregate

I am looking for some built in capability I don't know about.

Update
Please read my comment on Reed Copsey's answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我也只是我 2024-09-16 11:46:58

C#:

TimeSpan sum = times.Aggregate((t1, t2) => t1.Add(t2));

VB.NET:

Dim sum As TimeSpan = times.Aggregate(Function(t1, t2) t1.Add(t2))

C#:

TimeSpan sum = times.Aggregate((t1, t2) => t1.Add(t2));

VB.NET:

Dim sum As TimeSpan = times.Aggregate(Function(t1, t2) t1.Add(t2))
并安 2024-09-16 11:46:58

您在那里找到了答案 - 只需使用 TimeSpan.Add

如果您愿意,可以使用 LINQ 的 Enumerable.Aggregate 进行收集避免循环:

Dim sum as TimeSpan
sum = times.Aggregate(Function(ByVal current, ByVal ts) ts.Add(current) )

编辑:如果您想要扩展方法来执行此操作,您可以执行以下操作:

''
<Extension()> 
Public Function Aggregate(ByVal IEnumerable(Of TimeSpan) times) As TimeSpan
     Return times.Aggregate(Function(ByVal current, ByVal ts) ts.Add(current) )
End Function

You have the answer there - just use TimeSpan.Add.

You can do the collection using LINQ's Enumerable.Aggregate if you want to avoid the loop:

Dim sum as TimeSpan
sum = times.Aggregate(Function(ByVal current, ByVal ts) ts.Add(current) )

Edit: If you want an extension method to do this, you could do:

''
<Extension()> 
Public Function Aggregate(ByVal IEnumerable(Of TimeSpan) times) As TimeSpan
     Return times.Aggregate(Function(ByVal current, ByVal ts) ts.Add(current) )
End Function
远昼 2024-09-16 11:46:58

当然。

Enumerable.Aggregate 只需要 < code>Func—— 接受两个 T 对象并以某种方式聚合它们以产生新的 T 的东西。所以你可以使用尤里的方法

// The + operator is defined for TimeSpan, so you're fine just using that.
TimeSpan sum = times.Aggregate((t1, t2) => t1 + t2);

或者,你也可以做一些事情就像蒂姆·科克建议的一样,使用Enumerable.Sum 扩展方法:

TimeSpan sum = TimeSpan.FromTicks(times.Sum(t => t.Ticks));

更新:以下是 VB.NET 的等效项:

Dim sum = times.Aggregate(Function(t1, t2) t1 + t2)

Dim sum = TimeSpan.FromTicks(times.Sum(Function(t) t.Ticks))

Sure.

Enumerable.Aggregate just needs a Func<T, T, T> -- something that takes two T objects and aggregates them in some way to product a new T. So you can go with Yuriy's method:

// The + operator is defined for TimeSpan, so you're fine just using that.
TimeSpan sum = times.Aggregate((t1, t2) => t1 + t2);

Or, you can also do something like what Tim Coker suggested, using the Enumerable.Sum extension method:

TimeSpan sum = TimeSpan.FromTicks(times.Sum(t => t.Ticks));

Update: Here are the VB.NET equivalents:

Dim sum = times.Aggregate(Function(t1, t2) t1 + t2)

Dim sum = TimeSpan.FromTicks(times.Sum(Function(t) t.Ticks))
你在我安 2024-09-16 11:46:58

您可以使用 Sum 方法添加每个 TimeSpan 中的 Ticks 值:

Dim times = { _
  New TimeSpan(1, 0, 0), _
  New TimeSpan(1, 10, 0), _
  New TimeSpan(1, 50, 0), _
  New TimeSpan(0, 20, 0), _
  New TimeSpan(0, 10, 0) _
}

Dim t As New TimeSpan(times.Sum(Function(t) t.Ticks))

You can use the Sum method to add the Ticks value from each TimeSpan:

Dim times = { _
  New TimeSpan(1, 0, 0), _
  New TimeSpan(1, 10, 0), _
  New TimeSpan(1, 50, 0), _
  New TimeSpan(0, 20, 0), _
  New TimeSpan(0, 10, 0) _
}

Dim t As New TimeSpan(times.Sum(Function(t) t.Ticks))
说不完的你爱 2024-09-16 11:46:58

您需要对 TimeSpan.Ticks 求和,然后使用该值创建一个新的 TimeSpan

Dim times = 
{ 
    New TimeSpan(1, 0, 0), 
    New TimeSpan(1, 10, 0), 
    New TimeSpan(1, 50, 0), 
    New TimeSpan(0, 20, 0), 
    New TimeSpan(0, 10, 0) 
}

Dim sumTicks As Long = 0
For Each ts In times
    sumTicks += ts.Ticks
Next

Dim sum As New TimeSpan(sumTicks)

You need to sum TimeSpan.Ticks then create a new TimeSpan with that value

Dim times = 
{ 
    New TimeSpan(1, 0, 0), 
    New TimeSpan(1, 10, 0), 
    New TimeSpan(1, 50, 0), 
    New TimeSpan(0, 20, 0), 
    New TimeSpan(0, 10, 0) 
}

Dim sumTicks As Long = 0
For Each ts In times
    sumTicks += ts.Ticks
Next

Dim sum As New TimeSpan(sumTicks)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文