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)
答案 0 :(得分:4)
您正尝试将Intersect
的结果分配回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
。