enter code here想要从主列表中删除项目,但是给出错误'无法转换类型为<ExceptIterator>d__99'1[对象<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)
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)
最满意答案
您正在尝试将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 。
You're trying to assign the result of Intersect back to FieldCollectionToProcessList. The result isn't a List(Of FieldCollectionItem) - it's an IEnumerable(Of FieldCollectionItem). You'll need to call ToList if you need to turn it back into a list. You could do it all in one step though:
// Variable names changed for sanity list = list.Intersect(rejected, New FieldCollectionItemCompare). Intersect(accepted, New FieldCollectionItemCompare). ToListNote 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 use IntersectWith.
None of this has anything to do with your custom equality comparer, although I note that you could just use:
Return x.UniqueID = y.UniqueIDfor Equals, and
Return obj.UniqueIDfor GetHashCode.
更多推荐
发布评论