2012-06-15 41 views
0

enter code here想從主列表中刪除項目,但給出一個錯誤「無法轉換<ExceptIterator>d__99'1[類型的對象的IEqualityComparer(的對象)相交或不除工作

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 

請注意,如果您有選件嚴格的,你應該能夠看到錯誤在編譯時。

您可能還想考慮創建HashSet(Of FieldCollectionItem),然後使用IntersectWith

這都不是什麼與您的自定義相等比較,但我注意到,你可以只使用:

Return x.UniqueID = y.UniqueID 

Equals

Return obj.UniqueID 

GetHashCode