【问题标题】:Adding Values to Nested Dictionary向嵌套字典添加值
【发布时间】:2019-07-12 13:51:08
【问题描述】:

目前我正在尝试使用 VB 将值添加到嵌套字典中。我让它适用于平面字典,但无法完全理解嵌套的语法。

到目前为止,我已经评论了我遇到问题的行:

Public Shared Dim dictionary AS New System.Collections.Generic.Dictionary(Of String, System.Collections.Generic.Dictionary(Of String, Integer))

Function addValue(ByVal code AS String, ByVal cust AS String,ByVal value AS Integer)
    Dim innerDict AS New System.Collections.Generic.Dictionary(Of String, Integer)
    innerDict.Add(cust,value);
    IF dictionary.ContainsKey(code) Then
            IF dictionary.Item(code).ContainsKey(cust) Then 'Can I access the Customer key in this way? 
                    dictionary.Item(code).Item 'Here I need to update the value held by customer to the old value + new value. 
                Else    
                 dictionary(code).Add(cust,value)  'Is this syntax correct? 
            End If

        Else
            dictionary.Add(code,innerDict)
    End If
End Function

我想要的是有一个结构如下的字典:

 Code1:
      Customer1: 12
      Customer2: 13
    Code 2:
      Customer1: 12
      Customer2: 13

【问题讨论】:

    标签: vb.net reporting-services


    【解决方案1】:

    这是可以做你想做的事情的函数。

    它做的第一件事是检查codedictionary 中是否存在条目。如果不存在,它会添加一个值为空字典的字典,该字典将接收cust-value 对。

    目前该函数不返回任何值。如果不返回任何值,则应使用Sub

    Function addValue(ByVal code As String, ByVal cust As String, ByVal value As Integer)
    
        ' If no entry for code, create one.
        If Not dictionary.ContainsKey(code) Then
            dictionary.Add(code, New System.Collections.Generic.Dictionary(Of String, Integer))
        End If
        ' Add cust, value to entry at code.
        dictionary(code).Add(cust, value)
    
    End Function
    
    ' Returns sum the customer's values.
    Function SumCustomerValues(customer As String) As Integer
        Dim sum As Integer = 0
        For Each d As KeyValuePair(Of String, System.Collections.Generic.Dictionary(Of String, Integer)) In dictionary
            If d.Value.ContainsKey(customer) Then
                sum += d.Value(customer)
            End If
        Next
        Return sum
    End Function
    

    【讨论】:

    • 这似乎在做我想做的事,但是我如何访问内部字典中的值?像这样:dictionary(code).Item(cust) 我还需要将与代码和客户关联的值相加
    • @Ualati 我添加了一个函数来对Customer 的值求和。是的,要访问内部字典,您可以使用dictionary(code).Item(cust)dictionary(code)(cust),但您需要检查codecust 条目是否存在,否则会引发异常。
    • 我没有意识到在第一个函数中添加了一个客户条目,这很棒,但我使用字典作为总和。 (我知道这并不理想,但是 SSRS 在获取总数方面有奇怪的规则)在我的字典版本中,我没有嵌套,每次添加新字典时,我都有以下代码来更新值。 IF dictionary.ContainsKey(cust) Then dictionary(cust) = dictionary.Item(cust) + value Else dictionary.Add(cust,value) End If
    • 我的“解决方案”是做这样的事情If Not dictionary.ContainsKey(code) Then dictionary.Add(code, New System.Collections.Generic.Dictionary(Of String, Integer)) Else If dictionary(code).ContainsKey(cust) Then dictionary(code)(cust) = dictionary(code).Item(cust) + value End If End If
    猜你喜欢
    • 2020-12-06
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 2018-08-17
    相关资源
    最近更新 更多