1

I have this problem in vb.net. Lets say I got 2 Lists ListA and ListB both holds objects of same type.

Eg., one of the property of the object is ID. (ID is written in brackets)

 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.

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)

har07
  • 88,338
  • 12
  • 84
  • 137
Shiva
  • 235
  • 1
  • 4
  • 14
  • 1
    Well I don't think there is any OOB functionality, but [this question](http://stackoverflow.com/questions/4703867/how-would-i-compare-two-listsof-customclass-in-vb) and the accepted answer will probably help you. – Laurent S. Jul 09 '14 at 09:28
  • With Union, you could call the one that takes 2 parameter and pass an implementation of IEqualityComparer – the_lotus Jul 09 '14 at 12:47

4 Answers4

2

Use addRange() and then linq with distinct to filter out the duplicates.

Dim b = YourCollection.Distinct().ToList()
Archlight
  • 2,019
  • 2
  • 21
  • 34
0

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 
Greg d'Eon
  • 107
  • 1
  • 4
Sastreen
  • 597
  • 4
  • 13
0

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
GDutton
  • 164
  • 14
  • Thanks for your help. Is there a way to convert the Collection to a List because when I try to add the collection object to a List I get an Invalid cast Exception. Below is the Code I tried. For Each var As T In oCol AbschVar_List.Add(var) Next – Shiva Jul 11 '14 at 14:01
  • Check this out. http://stackoverflow.com/questions/243752/how-do-i-convert-a-listof-t-to-an-observablecollectionof-t-in-vb-net – GDutton Jul 12 '14 at 19:28
0
For each item as String in ListA 
   If Not ListB.Contains(item) Then 
      ListB.Add(item)
   End If 
Next
  • You should add some explanation to your answer to help the OP understand what your code is doing. i.e. your code is actually not generating a new list it is combining lists A and B into list B. This needs to be documented as part of your answer. – JRulle Nov 17 '14 at 21:58