0

I have a List of my Objects with many Properties.

Dim Duplicates As New List(Of ElementObject) Dim ListOfDuplicates As New List(Of List(Of ElementObject)) For Each Element As ElementObject In Duplicates Dim tmpList As List(Of ElementObject) 'Im looking for list of elements with the same width and height in Duplicates list tmpList = Duplicates.FindAll(Function(x) x.Width = Element.Width And x.Height = Element.Height) tmpList = tmpLista.OrderBy(Function(x) x.Id).ToList() 'Here is what I want: I want to look if tmpLista is already in ListOfDuplicates, but this code does not work If ListOfDuplicates.Contains(tmpList) Then Continue For End If ListOfDuplicates.Add(tmpList) Next 

How can I achieve this, to check if List of my another List of Objects contains that list already?

5
  • tmpLista in tmpList = tmpLista.OrderBy(Function(x) x.Id).ToList() appears to be a typo. Commented Mar 29, 2016 at 6:31
  • No no, I just changed a naming of my variables and forgot to change tmpList :) Commented Mar 29, 2016 at 6:34
  • Your code can't work, since tmpList is a newly created list every time you do the check. Commented Mar 29, 2016 at 7:23
  • I think what you are really trying to do here can be achieved much more elegantly and easy with a single call to Duplicates.GroupBy(...) Commented Mar 29, 2016 at 7:24
  • @JakobOlsen, I know it could be resolved with much better style as you said, but it is really not my code and I just wanted to add this functionality to it Commented Mar 29, 2016 at 8:08

2 Answers 2

1

You can use SequenceEqual to compare the contents of the list with two caveats:

  • The lists need to be sorted the same way
  • The objects in the lists have to be equatable. You can do this by overriding Equals or by implementing a function that checks for equality.

I'm more used to this in C# where the lambda expressions are easier to write and I can create an extension class without creating a separate module. This class provides the hash code calculation and comparisons to check two lists of elements for the same values. It calculates a set of hash codes for each list, sorts the hash codes, and then compares the sets of hash codes using SequenceEquals.

Public Class ElementListComparer Public Shared Function ListsAreEqual(list1 As IEnumerable(Of Element), list2 As IEnumerable(Of Element)) If list1 Is Nothing OrElse list2 Is Nothing Then Return False If Object.ReferenceEquals(list1, list2) Then Return True return GetSortedHashCodes(list1).sequencequal(GetSortedHashCodes(list2)) End Function Public Shared Function GetSortedHashCodes(elements As IEnumerable(Of Element)) Return elements.Select(Function(el As Element) As Long Return CalculateHashCode(el) End Function).OrderBy(Function(hashcode) Return hashcode End Function) End Function Public Shared Function CalculateHashCode(el As Element) As Long Return (el.Height * 397) ^ el.Width End Function End Class 

So you would call

ElementListComparer.ListsAreEqual(list1 as IEnumerable(of Element), list2 as IEnumerable(of Element)) 
Sign up to request clarification or add additional context in comments.

Comments

0

This did the trick:

Dim Contain As Boolean = ListOfDuplicates.Any(Function(x) x.SequenceEqual(tmpList)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.