Results 1 to 5 of 5

Thread: Getting the difference between two lists Expandoobjects with the same properties.

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    3

    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!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,349

    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:
    1. Public Class ExpandoObjectEqualityComparer
    2.     Implements IEqualityComparer(Of Object)
    3.  
    4.     Public Function GetHashCode(obj As Object) As Integer Implements IEqualityComparer(Of Object).GetHashCode
    5.         Return obj.Text.GetHashCode() Xor obj.Number.GetHashCode()
    6.     End Function
    7.  
    8.     Private Function IEqualityComparer_Equals(x As Object, y As Object) As Boolean Implements IEqualityComparer(Of Object).Equals
    9.         Return x.Text = y.Text AndAlso x.Number = y.Number
    10.     End Function
    11.  
    12. End Class
    and:
    vb.net Code:
    1. Dim obj1 As Object = New ExpandoObject
    2. Dim obj2 As Object = New ExpandoObject
    3. Dim obj3 As Object = New ExpandoObject
    4.  
    5. obj1.Text = "Hello World"
    6. obj1.Number = 123
    7.  
    8. obj2.Text = "Goodbye World"
    9. obj2.Number = 456
    10.  
    11. obj3.Text = "Hello World"
    12. obj3.Number = 123
    13.  
    14. Dim allItems As New List(Of Object) From {obj1, obj2, obj3}
    15. Dim uniqueItems As New HashSet(Of Object)(New ExpandoObjectEqualityComparer)
    16.  
    17. Console.WriteLine("Adding unique items:")
    18.  
    19. For Each item In allItems
    20.     If uniqueItems.Add(item) Then
    21.         Console.WriteLine("New item added")
    22.     Else
    23.         Console.WriteLine("Duplicate item rejected")
    24.     End If
    25. Next
    26.  
    27. Console.WriteLine("Listing unique items:")
    28.  
    29. For Each item In uniqueItems
    30.     Console.WriteLine($"Text: '{item.Text}'; Number: {item.Number}")
    31. Next

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,349

    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:
    1. Dim obj1 As Object = New ExpandoObject
    2. Dim obj2 As Object = New ExpandoObject
    3. Dim obj3 As Object = New ExpandoObject
    4.  
    5. obj1.Text = "Hello World"
    6. obj1.Number = 123
    7.  
    8. obj2.Text = "Goodbye World"
    9. obj2.Number = 456
    10.  
    11. obj3.Text = "Hello World"
    12. obj3.Number = 123
    13.  
    14. Dim list1 As New List(Of Object) From {obj1, obj2}
    15. Dim list2 As New List(Of Object) From {obj3}
    16. Dim difference = list1.Except(list2, New ExpandoObjectEqualityComparer)
    17.  
    18. For Each item In difference
    19.     Console.WriteLine($"Text: '{item.Text}'; Number: {item.Number}")
    20. Next

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    3

    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!

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,746

    Re: Getting the difference between two lists Expandoobjects with the same properties.

    Quote Originally Posted by Pdevine74 View Post
    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)Ö

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width