Search code examples
vb.netlinqdistinctiequalitycompareriequatable

LINQ Distinct Not Working


I'm trying to select distinct instances from my class but it's not working, even after implements IEqualityComparer (as described here: https://msdn.microsoft.com/en-us/library/bb338049.aspx) AND IEquatable

Could someone help?

Public Class Teste
  Implements IEquatable(Of Teste), IEqualityComparer(Of Teste)
  Private _Codigo As Integer
  Public Property Codigo() As Integer
    Get
      Return _Codigo
    End Get
    Set(ByVal value As Integer)
      _Codigo = value
    End Set
  End Property
  Public Sub New(id As Integer)
    Me.Codigo = id
  End Sub

  Public Overrides Function Equals(obj As Object) As Boolean
    Return DirectCast(obj, Teste).Codigo = Me.Codigo
  End Function

  Public Function Equals1(other As Teste) As Boolean Implements IEquatable(Of Teste).Equals
    Return Me.Codigo = other.Codigo
  End Function

  Public Function Equals2(x As Teste, y As Teste) As Boolean Implements IEqualityComparer(Of Teste).Equals
    Return x.Codigo = y.Codigo
  End Function

  Public Function GetHashCode1(obj As Teste) As Integer Implements IEqualityComparer(Of Teste).GetHashCode
    Return DirectCast(obj, Teste).Codigo
  End Function
End Class

Testing:

Dim p1 As New Teste(78)
Dim p2 As New Teste(78)
Dim l As New List(Of Teste) From {p1, p2}
MsgBox(l.Count())
MsgBox(l.Distinct().Count())

I've got two msgbox both showing "2".


Solution

  • You need to tell the Distinct method what equality comparer to use. I would also split that out into it's own class. For example:

    Public Class TesteEqualityComparer 
        Implements IEqualityComparer(Of Teste)
    
        Public Function Equals2(x As Teste, y As Teste) As Boolean Implements IEqualityComparer(Of Teste).Equals
            Return x.Codigo = y.Codigo
        End Function
    
        Public Function GetHashCode1(obj As Teste) As Integer Implements IEqualityComparer(Of Teste).GetHashCode
            Return obj.Codigo.GetHashCode()
        End Function
    End Class
    

    And now your query is:

    MsgBox(l.Distinct(New TesteEqualityComparer()).Count())