如果 IEqualityComparer 根据结果集中的单个字段的值排除某些结果,我正在尝试使用自定义实现对 LINQ 结果集使用“Except”方法。
所以,以简化的形式,我有......
'' 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)
我的比较器如下...
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
我在“.Except”行收到一个无效的强制转换异常,并显示消息“无法将'...insCompare'类型的对象强制转换为'System.Collections.Generic.IEqualityComparer'”
任何人都可以解释为什么这可能是请。