Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net LINQ不工作_Vb.net_Linq_Distinct_Iequalitycomparer_Iequatable - Fatal编程技术网

Vb.net LINQ不工作

Vb.net LINQ不工作,vb.net,linq,distinct,iequalitycomparer,iequatable,Vb.net,Linq,Distinct,Iequalitycomparer,Iequatable,我试图从类中选择不同的实例,但它不起作用,即使在实现了IEqualityComparer(如下所述)和IEquatable之后也是如此 有人能帮忙吗 Public Class Teste Implements IEquatable(Of Teste), IEqualityComparer(Of Teste) Private _Codigo As Integer Public Property Codigo() As Integer Get Return _Codi

我试图从类中选择不同的实例,但它不起作用,即使在实现了IEqualityComparer(如下所述)和IEquatable之后也是如此

有人能帮忙吗

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
测试:

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())

我有两个msgbox都显示为“2”。

您需要告诉
Distinct
方法要使用什么等式比较器。我也会把它分成自己的类。例如:

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
现在您的查询是:

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

您需要告诉
Distinct
方法要使用什么等式比较器。我也会把它分成自己的类。例如:

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
现在您的查询是:

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

对于hashcode,DirectCast是不需要的(我猜是OP代码的遗留部分)。@AndrewMorton确实如此,但我已经有7/8年没有使用VB.Net了,所以我没有碰它@AndrewMorton对其进行了编辑,并对其进行了进一步改进,使用了
Codigo
属性,因为hashcode是顽皮的。我的印象是,一个整数完全可以用作它自己的hashcode,例如,因为hashcode本身是一个整数,没有发生冲突的机会。对于hashcode,不需要DirectCast(我猜是OP的代码留下的)@AndrewMorton确实如此,但我已经7/8年没有使用VB.Net了,所以我没有碰它!@AndrewMorton编辑了它,并对它进行了一些改进,使用
Codigo
属性,因为hashcode很调皮。我的印象是,整数完全可以用作它自己的hashcode,例如,因为hashcode本身是一个整数,而碰撞的可能性为零。