-
May 13th, 2022, 08:39 PM
#1
Thread Starter
New Member
Getting the difference between two lists Expandoobjects with the same properties.
Hello all.
I have been googling for what seems hours trying to figure this out. So I have two lists(of Expandoobjects). The objects in both lists contain the same properties. The first list Is considered a master list where I am trying to keep the objects unique. The second list will contain duplicate objects as well as new objects which I would like to add to the first list (master unique list). I am new to using expando objects so I am struggling with how to find the delta between the two lists so I can add them to the master list. Can anyone offer any suggestions? It would be greatly appreciated it and thanks for taking the time to read this post. Have a great day!
btw. I am using .net core6 for my application.
Thanks!
-
May 13th, 2022, 11:01 PM
#2
Re: Getting the difference between two lists Expandoobjects with the same properties.
Maybe you don't have to. If you use a HashSet(Of T) instead of a List(Of T) then you can simply add whatever objects you like and it will reject any duplicates automatically. By default, reference equality will be used to determine uniqueness, but you can provide your own IEqualityComparer(Of T) when you create the list to implement your own value equality comparison if you wish. E.g.
vb.net Code:
Public Class ExpandoObjectEqualityComparer
Implements IEqualityComparer(Of Object)
Public Function GetHashCode(obj As Object) As Integer Implements IEqualityComparer(Of Object).GetHashCode
Return obj.Text.GetHashCode() Xor obj.Number.GetHashCode()
End Function
Private Function IEqualityComparer_Equals(x As Object, y As Object) As Boolean Implements IEqualityComparer(Of Object).Equals
Return x.Text = y.Text AndAlso x.Number = y.Number
End Function
End Class
and:
vb.net Code:
Dim obj1 As Object = New ExpandoObject
Dim obj2 As Object = New ExpandoObject
Dim obj3 As Object = New ExpandoObject
obj1.Text = "Hello World"
obj1.Number = 123
obj2.Text = "Goodbye World"
obj2.Number = 456
obj3.Text = "Hello World"
obj3.Number = 123
Dim allItems As New List(Of Object) From {obj1, obj2, obj3}
Dim uniqueItems As New HashSet(Of Object)(New ExpandoObjectEqualityComparer)
Console.WriteLine("Adding unique items:")
For Each item In allItems
If uniqueItems.Add(item) Then
Console.WriteLine("New item added")
Else
Console.WriteLine("Duplicate item rejected")
End If
Next
Console.WriteLine("Listing unique items:")
For Each item In uniqueItems
Console.WriteLine($"Text: '{item.Text}'; Number: {item.Number}")
Next
-
May 13th, 2022, 11:06 PM
#3
Re: Getting the difference between two lists Expandoobjects with the same properties.
If you don't want to use a HashSet(Of T), you can still use the same equality comparer to determine the difference between two lists using the Except extension method:
vb.net Code:
Dim obj1 As Object = New ExpandoObject
Dim obj2 As Object = New ExpandoObject
Dim obj3 As Object = New ExpandoObject
obj1.Text = "Hello World"
obj1.Number = 123
obj2.Text = "Goodbye World"
obj2.Number = 456
obj3.Text = "Hello World"
obj3.Number = 123
Dim list1 As New List(Of Object) From {obj1, obj2}
Dim list2 As New List(Of Object) From {obj3}
Dim difference = list1.Except(list2, New ExpandoObjectEqualityComparer)
For Each item In difference
Console.WriteLine($"Text: '{item.Text}'; Number: {item.Number}")
Next
-
May 16th, 2022, 02:45 PM
#4
Thread Starter
New Member
Re: Getting the difference between two lists Expandoobjects with the same properties.
jmcilhinney,
This worked perfectly! Thank you so much for taking the time to reply and help me out. I now have a better understanding of how Expandoobjects work. You are a rockstar!
-
Re: Getting the difference between two lists Expandoobjects with the same properties.
 Originally Posted by Pdevine74
jmcilhinney,
This worked perfectly! Thank you so much for taking the time to reply and help me out. I now have a better understanding of how Expandoobjects work. You are a rockstar!
That’s not how ExpandoObjects work. That’s how EqualityComparers and Linq work. The same methods would work with any List(Of Custom Class)…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|