我想在使用自定義實現的LINQ結果集上使用「Except」方法,如果IEqualityComparer根據結果集中單個字段的值排除某些結果。LINQ除了使用自定義比較器
因此,以簡單的形式我有...
'' Get collection of published sites...
Dim List1 = (From i In db.Sites _
Where (i.StatusID = published) _
Select i.SiteID, _
i.SiteName)
'' Find those with a pending site, but exclue all those whose SiteID is in List1...
Dim insComparer = New insCompare
Dim List2 = (From i In db.Sites _
Where (i.StatusID = pending) _
Select i.SiteID, _
i.SiteName).Except(List1, insComparer)
我的Comparer是如下...
Public Class insCompare
Implements System.Collections.Generic.IEqualityComparer(Of Object)
Public Function Equals1(ByVal x As Object, ByVal y As Object) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of Object).Equals
Return IIf(x.SiteID = y.SiteID, True, False)
End Function
Public Function GetHashCode1(ByVal x As Object) As Integer Implements System.Collections.Generic.IEqualityComparer(Of Object).GetHashCode
Return x.SiteID.ToString.ToLower.GetHashCode()
End Function
End Class
我上 「除」 行了無效的轉換異常帶消息「無法將類型爲'...比較'的對象類型轉換爲鍵入'System.Collections.Generic.IEqualityComparer'」
有沒有人可以投射爲什麼這可能是請。
是的,這是我開始的地方,但我得到了同樣的錯誤。我認爲,因爲它說它不能通過類來轉換爲「System.Collections.Generic.IEqualityComparer」,這是它想要的? – FourOaks 2009-06-17 12:27:29