如何在 vb.net 中合并两个列表以获得一个没有重复值的不同列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24649889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to merge two list to have a distinct list without duplicate values in vb.net
提问by Shiva
I have this problem in vb.net. Lets say I got 2 Lists ListA and ListB both holds objects of same type.
我在 vb.net 中有这个问题。假设我有 2 个列表 ListA 和 ListB 都包含相同类型的对象。
Eg., one of the property of the object is ID. (ID is written in brackets)
例如,对象的属性之一是 ID。(括号内写的是ID)
ListA ListB
---------------------------
A(3818) A(3818)
B(3819) B(3819)
C(3820) C(3820)
D(3821) D(3821)
E(3823) F(0)
H(3824) G(0)
I(3825)
How do I merge these two Lists to have a new distinct list which holds objects only once whose ID matches and all other objects(whose ID dont match) are simply added to the new list.
我如何合并这两个列表以获得一个新的不同列表,该列表仅包含一次其 ID 匹配的对象,而所有其他对象(其 ID 不匹配)只是简单地添加到新列表中。
Sample output be,
样本输出为,
New List
--------
A(3818)
B(3819)
C(3820)
D(3821)
E(3823)
F(0)
G(0)
H(3824)
I(3825)
When I searched I found that AddRange() and Union are some of the methods to do the merge. But i am not able to find if this works for non standard objects(apart from Integer, String)
当我搜索时,我发现 AddRange() 和 Union 是进行合并的一些方法。但我无法找到这是否适用于非标准对象(除了整数、字符串)
采纳答案by GDutton
Could use a collection bucket
可以使用收集桶
Dim oCol As New Collection
AddTitems(oCol, oListA)
AddTitems(oCol, olistB)
Public Function AddTitems(oSummaryList As Collection, oList As List(Of thing)) As Collection
For Each oThing As thing In oList
If Not oSummaryList.Contains(CStr(oThing.ID)) Then oSummaryList.Add(oList, CStr(oThing.ID))
Next
Return oSummaryList
End Function
回答by Archlight
Use addRange() and then linq with distinct to filter out the duplicates.
使用 addRange() 然后使用 distinct 的 linq 过滤掉重复项。
Dim b = YourCollection.Distinct().ToList()
回答by Sastreen
Here are a couple simple functions that should do that for you. I'm not sure how efficient they are though. I don't think there is anything built in.
这里有几个简单的函数可以为您做到这一点。我不确定它们的效率如何。我不认为有任何内置的东西。
Private Function nameOfFunction(list1 as list(of type), list2 as list(of type)) as list(of type)
Dim result as new list(of type)
for a as integer = 0 to math.max(list1.count, list2.count) - 1 step 1
If a < list1.count AndAlso resultHasID(result, list1(a).ID) = False Then
result.add(list1(a))
end if
If a < list2.count AndAlso resultHasID(result, list2(a).ID) = False Then
result.add(list2(a))
end if
next
End Function
Private Function resultHasID(testList as list(of type), s as string) as boolean
Dim result as Boolean = False
for a as integer = 0 to testlist.count - 1 step 1
if(testlist(a).ID = s) then
result = true
exit for
End if
Next
Return result
End function
回答by Allan Blackford
For each item as String in ListA
If Not ListB.Contains(item) Then
ListB.Add(item)
End If
Next

