enter code hereWant to remove items from a Main list , but give an error ‘Unable to cast object of type <ExceptIterator>d__99'1[‘
Public Class FieldCollectionItemCompare
Implements System.Collections.Generic.IEqualityComparer(Of FieldCollectionItem)
Public Shadows Function Equals(ByVal x As FieldCollectionItem, ByVal y As FieldCollectionItem) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of FieldCollectionItem).Equals
If x.UniqueID = y.UniqueID Then
Equals = True
Else
Equals = False
End If
End Function
Public Overloads Function GetHashCode(ByVal obj As FieldCollectionItem) As Integer Implements System.Collections.Generic.IEqualityComparer(Of FieldCollectionItem).GetHashCode
GetHashCode = obj.UniqueID + obj.UniqueID
End Function
End Class
…here is what I am doing
FieldCollectionToProcessList = FieldCollectionToProcessList.Intersect(FieldCollectionRejected, New FieldCollectionItemCompare)
FieldCollectionToProcessList = FieldCollectionToProcessList.Intersect(FieldCollectionAccepted, New FieldCollectionItemCompare)
…all the list are As Generic.List(Of FieldCollectionItem)
You’re trying to assign the result of
Intersectback toFieldCollectionToProcessList. The result isn’t aList(Of FieldCollectionItem)– it’s anIEnumerable(Of FieldCollectionItem). You’ll need to callToListif you need to turn it back into a list. You could do it all in one step though:Note that if you have Option Strict on, you should be able to see the error at compile time.
You might also want to consider creating a
HashSet(Of FieldCollectionItem), then useIntersectWith.None of this has anything to do with your custom equality comparer, although I note that you could just use:
for
Equals, andfor
GetHashCode.