【问题标题】:Sum integer value fields of a dictionary in vb.net 4.0在 vb.net 4.0 中对字典的整数值字段求和
【发布时间】:2012-01-06 12:31:44
【问题描述】:

在 .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

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

【问题讨论】:

    标签: .net vb.net dictionary


    【解决方案1】:

    只需使用Sum() 方法:

    dbar.Values.Sum()
    

    【讨论】:

      【解决方案2】:

      是的,使用 LINQ to Objects:

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

      【讨论】:

        猜你喜欢
        • 2020-09-19
        • 1970-01-01
        • 1970-01-01
        • 2016-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-21
        相关资源
        最近更新 更多