繁体   English   中英

vb.net 4.0中字典的求和整数值字段

[英]Sum integer value fields of a dictionary in vb.net 4.0

在.net 4.0字典中我知道我可以像这样计算所有整数值字段的总和:

 Dim dbar As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)() From {{"A", 1}, {"B", 1}, {"C", 1}}
    Dim sum As Integer = 0
    For Each v As Integer In dbar.Values
      sum += v
    Next

是否存在更优雅的方式来计算总和?

只需使用Sum()方法:

dbar.Values.Sum()

是的,使用LINQ to Objects:

Dim sum As Integer = dbar.Values.Sum()

暂无
暂无

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

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