我是vb.net环境的新手,我需要一些帮助来弄清楚是否有一种简单的方法来合并来自相同List(对象)的对象,这些对象在一个字段上具有相同的值(在这种情况下它将是rulenumber)。
我没有准备好代码我只需首先弄清楚是否有办法以及如何继续以构建此功能。
所以计划是我有一个(对象)列表,其中包含许多对象条目,其中包含像rulenumber金额计数符号这样的字段。 在该列表中,我有许多具有相同rulenumber的条目。我想创建一个新的(对象)列表,并在一个条目中将所有对象与相同的rulenumber字段组合。
请告知。
答案 0 :(得分:0)
那将是6瓶啤酒,谢谢: - )
Class myCustomList
Property objectList As New List(Of mycustomobject)
Class mycustomobject
Property thisIsmyProp As String
End Class
Public Sub _add_object(objectToAdd As mycustomobject)
For i = 0 To objectList.Count - 1
If objectToAdd.thisIsmyProp = objectList(i).thisIsmyProp Then
'this object exists... merge it in
objectList(i) = objectToAdd
Exit Sub
End If
Next
'object was not found, add it..
objectList.Add(objectToAdd)
End Sub
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim mylistOfObjects As New myCustomList
Dim myNewObject As New myCustomList.mycustomobject
myNewObject.thisIsmyProp = "12345"
mylistOfObjects._add_object(myNewObject)
myNewObject = New myCustomList.mycustomobject
myNewObject.thisIsmyProp = "56789"
mylistOfObjects._add_object(myNewObject)
myNewObject = New myCustomList.mycustomobject
myNewObject.thisIsmyProp = "12345"
mylistOfObjects._add_object(myNewObject)
Debug.Print(mylistOfObjects.objectList.Count)
End Sub