晚上好。
我正在尝试对列表中的某些元素进行深层复制,而我发现这样做非常困难。我正在尝试许多新概念,如果以下某些代码看起来很奇怪,请耐心等待...
我有以下列表:
accountList as List(Of Account)
accountsInArea as New List(Of Account)
result as New List(Of Account)
其中每个元素的类型都是我自己定义的类:
Public Class Account
Enum AddingType
income
expenditure
End Enum
Enum AreaType
high
mid
low
End Enum
Property Area() As AreaType
Property Group() As String
Property AccountNumber() As Integer
Property AccountDescription() As String
Property IncomeExpenditure() As AddingType
Public Property MonthlyValues As New Dictionary(Of Integer, Decimal)
End Class
我需要找到accountList
的某些项并将值复制到新列表:accountsInArea
。目前,我已经使用以下方法:
accountsInArea = accountList.FindAll(Function(acc) acc.Area = area)
但是,这会生成Lambda表达式生成的那些项的reference的副本(不是吗?)。如果在SO和MSDN上浏览时我理解正确,那么我需要的是元素的deep copy。
我想让accountsInArea
拥有.FindAll
的所有物品的副本,包括(尤其是)MonthlyValues
词典中的物品。
我尝试过:
accountsInArea = accountList.Select(accountList.FindAll(Function(acc) acc.Area = area))
也:
For Each account In accountList.FindAll(Function(acc) acc.Area = area)
accountsInArea.Add(account)
Next
最后添加到类中:
Implements ICloneable
Public Function Clone() As Object Implements System.ICloneable.Clone
Return Me.MemberwiseClone
End Function
并在方法内部使用:
accountsInArea = accountList.FindAll(Function(acc) acc.Area = area)
result = accountsInArea.Select(Function(acc) acc.Clone()).Cast(Of Account).ToList
非常感谢您的帮助。
[修补了一些之后,我找到了解决方案。尽管我敢肯定有一些更优雅的方法,但是如果有人迫切需要,我会在这里发布:
我最终在方法中使用了以下代码:
Dim accountsInArea as New List(Of Account)
For Each account In accountList.FindAll(Function(acc) acc.Area = area)
Dim a As New Account
With a
.Area = account.Area
.AccountDescription = account.AccountDescription
.Group = account.Group
.IncomeExpenditure = account.IncomeExpenditure
.AccountNumber = account.AccountNumber
.MonthlyValues = New Dictionary(Of Integer, Decimal)
End With
For Each val In account.MonthlyValues
Dim k As Integer = v.Key
Dim v As Decimal = v.Value
a.MonthlyValues.Add(k, v)
Next
accountsInArea.Add(a)
Next
因此,如果有人知道一种不太复杂的方法(可能涉及到我在问题中提到的一种方法),那将是很棒的。
仍然感谢。
只需在您的类中实现ICloneable,然后
Dim accountsInArea As List(Of Account) = accountList.Where(Function(x) x.Area = Account.AreaType.high).Select(Function(y) y.clone).Cast(Of Account)().ToList
这里是您以前的完整代码
Sub AccountListProg
Dim accountList As New List(Of Account) From {New Account With {.Area = Account.AreaType.high, .AccountNumber = 1}, _
New Account With {.Area = Account.AreaType.high, .AccountNumber = 2}, _
New Account With {.Area = Account.AreaType.low, .AccountNumber = 3}, _
New Account With {.Area = Account.AreaType.mid, .AccountNumber = 4}}
Dim accountsInArea As List(Of Account) = accountList.Where(Function(x) x.Area = Account.AreaType.high).Select(Function(y) y.clone).Cast(Of Account)().ToList
End Sub
Public Class Account
Implements ICloneable
Public Function Clone() As Object Implements ICloneable.Clone
Return New Account With {.AccountDescription = AccountDescription, _
.AccountNumber = AccountNumber, _
.Area = Area, _
.Group = Group, _
.IncomeExpenditure = AddingType.income, _
.MonthlyValues = MonthlyValues}
End Function
Enum AddingType
income
expenditure
End Enum
Enum AreaType
high
mid
low
End Enum
Property Area() As AreaType
Property Group() As String
Property AccountNumber() As Integer
Property AccountDescription() As String
Property IncomeExpenditure() As AddingType
Public Property MonthlyValues As New Dictionary(Of Integer, Decimal)
End Class