IEqualityComparer(对象)用于相交或不工作(IEqualityComparer(of object) for Intersect or Except not working)

系统教程 行业动态 更新时间:2024-06-14 16:57:39
IEqualityComparer(对象)用于相交或不工作(IEqualityComparer(of object) for Intersect or Except not working)

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). ToList

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 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.UniqueID

for Equals, and

Return obj.UniqueID

for GetHashCode.

更多推荐

本文发布于:2023-04-13 12:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/0a9c3a77db1569db3f56de2674047c10.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:或不   对象   工作   IEqualityComparer   Intersect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!