我正在尝试遍历 DataTable 并区分机会是否导致销售 - 但机会发生的途径有多种,我想按用户组织我的输出。
我有一本字典,键是用户,值字段是自定义数据结构。最初添加键/值时,事情似乎表现正常,但是当更新字典中已经存在的值时,不会保留更改。我的自定义数据类型似乎在内部更新它的值,但是当函数调用退出时,字典的值保持不变。
使用的数据结构有:
Public Structure ColleagueSlogs
Public Call As LoggedItems
Public Email As LoggedItems
End Structure
Public Structure LoggedItems
Public SalesLogged As Integer
Public Sub IncrementSales()
Me.SalesLogged += 1
End Sub
Public NonSalesLogged As Integer
Public Sub IncrementNonSales()
Me.NonSalesLogged += 1
End Sub
End Structure
调用函数,为清楚起见略微简化:
Protected Function SortData(ByVal data As DataTable) As Dictionary(Of String, ColleagueSlogs)
Dim tmpDict As New Dictionary(Of String, ColleagueSlogs)
For Each result As DataRow In data.Rows
Dim tmpName As String = result.Item("UserID")
If tmpDict.ContainsKey(tmpName) Then ''This block does not update correctly
'this exists - increment the relevant variable
Select Case result.Item("Origin")
Case "Call"
Select Case result.Item("Sale")
Case "Yes"
tmpDict(tmpName).Call.IncrementSales()
Case "No"
tmpDict(tmpName).Call.IncrementNonSales()
End Select
Case "Email"
Select Case result.Item("Sale")
Case "Yes"
tmpDict(tmpName).Email.IncrementSales()
Case "No"
tmpDict(tmpName).Email.IncrementNonSales()
End Select
End Select
Else ''This block works as expected
'create data structure, increment the relevant var and add it to dict
Dim tmpSlogs As New ColleagueSlogs
Select Case result.Item("Origin")
Case "Call"
Select Case result.Item("Sale")
Case "Yes"
tmpSlogs.Call.IncrementSales()
Case "No"
tmpSlogs.Call.IncrementNonSales()
End Select
Case "Email"
Select Case result.Item("Sale")
Case "Yes"
tmpSlogs.Email.IncrementSales()
Case "No"
tmpSlogs.Email.IncrementNonSales()
End Select
End Select
tmpDict.Add(tmpName, tmpSlogs)
End If
Next
Return tmpDict
End Function
tmpDict(tmpName).Call.SalesLogged += 1
是一个值,不能成为赋值的目标 - 有没有办法防止字典的值表现得像ReadOnly
值?
问题是否在于我的自定义数据类型的定义?还是我应该寻找一种完全不同的方法来解决这个问题?