我有一个包含许多属性的对象列表。
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
如何实现这一点,检查我的另一个对象列表的列表是否已包含该列表?
答案 0 :(得分:1)
您可以使用Equals
将列表内容与两个警告进行比较:
SequenceEquals
或通过实现检查相等性的函数来完成此操作。我更习惯于在C#中使用lambda表达式更容易编写,我可以在不创建单独模块的情况下创建扩展类。此类提供哈希码计算和比较,以检查相同值的两个元素列表。它为每个列表计算一组哈希码,对哈希码进行排序,然后使用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
比较哈希码集。
ElementListComparer.ListsAreEqual(list1 as IEnumerable(of Element), list2 as IEnumerable(of Element))
所以你打电话给
rake db:migrate
答案 1 :(得分:0)
这就是诀窍:
Dim Contain As Boolean = ListOfDuplicates.Any(Function(x) x.SequenceEqual(tmpList))