0

enter code here想要从主列表中删除项目,但给出错误“无法转换类型的对象<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

...这就是我正在做的事情

FieldCollectionToProcessList = FieldCollectionToProcessList.Intersect(FieldCollectionRejected, New FieldCollectionItemCompare)

FieldCollectionToProcessList = FieldCollectionToProcessList.Intersect(FieldCollectionAccepted, New FieldCollectionItemCompare)

...所有列表都是 As Generic.List(Of FieldCollectionItem)

4

1 回答 1

4

您正在尝试将Intersectback 的结果分配给FieldCollectionToProcessList. 结果不是List(Of FieldCollectionItem)- 它是IEnumerable(Of FieldCollectionItem). ToList如果您需要将其转回列表,则需要致电。您可以一步完成所有操作:

// Variable names changed for sanity
list = list.Intersect(rejected, New FieldCollectionItemCompare).
            Intersect(accepted, New FieldCollectionItemCompare).
            ToList

请注意,如果您启用了 Option Strict,您应该能够在编译时看到错误。

您可能还想考虑创建一个HashSet(Of FieldCollectionItem),然后使用IntersectWith.

这些都与您的自定义相等比较器无关,尽管我注意到您可以只使用:

Return x.UniqueID = y.UniqueID

对于Equals, 和

Return obj.UniqueID

GetHashCode.

于 2012-06-15T17:00:54.873 回答