I basically have 2 list of the same object type.
The first one (A,B,C,D) is a property of one of my objects.
I need to join the second list(B, E, F) in my object, but exclude the duplicates.
This means i can’t just do:
ListA.AddRange(ListB)
I’ll have to change it to
ForEach item in ListB
If Not ListA.Contains(item)
ListA.Add(item)
EndIf
Next
Or add:
ListA = ListA.Distinct()
Is there a faster and smoother way to code this?
If you have already overridden the
EqualsandGetHashcodeyou can useEnumerable.Uniondirectly:Otherwise you could implement
IEqulityCompararer<Foo>and useListA.Union(ListB, comparer).Assuming
Foois the type of your class andNameis a property you want to use to detect duplicates.Now use this comparer for
Union: