none
请问:自定义类怎样使用Linq的Distinct方法? RRS feed

  • 问题

  • 我自定义了一个类,请问怎么能对这个类的List使用Linq的Distinct方法(如果point1.x=point2.x,就认为两个实例一样)

    Public Class Dimension Public point1 As New Drawing.PointF Public point2 As New Drawing.PointF End Class

    Public Class Form1
        Public DimList As New List(Of Dimension)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       End Sub

    End Class


    2018年9月30日 1:21

答案

  • Hi,

    try the code:

    Public Class ProductIdComparer
        Implements IEqualityComparer(Of Drawing.PointF)
    
        Public Function Equals(x As PointF, y As PointF) As Boolean Implements IEqualityComparer(Of PointF).Equals
            If x = Nothing Then Return y = Nothing
            Return x.X = y.X
        End Function
    
        Public Function GetHashCode(obj As PointF) As Integer Implements IEqualityComparer(Of PointF).GetHashCode
            If obj = Nothing Then Return 0
            Return obj.X.GetHashCode()
        End Function
    End Class
    Public Class Form1
    
        Dim product As List(Of Drawing.PointF) = New List(Of Drawing.PointF)() From {
        New Drawing.PointF() With {
            .X = 1,
            .Y = 1
        },
        New Drawing.PointF() With {
            .X = 1,
            .Y = 2
        },
        New Drawing.PointF() With {
            .X = 4,
            .Y = 3
        },
         New Drawing.PointF() With {
            .X = 5,
            .Y = 4
        }
    }
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim distinctProduct = product.Distinct(New ProductIdComparer())
            
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • 已编辑 Alex-KSGZ 2018年9月30日 8:16
    • 已标记为答案 RF8862 2018年10月1日 3:27
    2018年9月30日 8:15

全部回复

  • 通過自訂  IEqualityComparer<T> 使用 Distinct<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>) 方法

    參考

    https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.enumerable.distinct?view=netframework-4.7.2#System_Linq_Enumerable_Distinct__1_System_Collections_Generic_IEnumerable___0__System_Collections_Generic_IEqualityComparer___0__


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。 https://skilltree.my/

    2018年9月30日 6:59
  • Hi,

    try the code:

    Public Class ProductIdComparer
        Implements IEqualityComparer(Of Drawing.PointF)
    
        Public Function Equals(x As PointF, y As PointF) As Boolean Implements IEqualityComparer(Of PointF).Equals
            If x = Nothing Then Return y = Nothing
            Return x.X = y.X
        End Function
    
        Public Function GetHashCode(obj As PointF) As Integer Implements IEqualityComparer(Of PointF).GetHashCode
            If obj = Nothing Then Return 0
            Return obj.X.GetHashCode()
        End Function
    End Class
    Public Class Form1
    
        Dim product As List(Of Drawing.PointF) = New List(Of Drawing.PointF)() From {
        New Drawing.PointF() With {
            .X = 1,
            .Y = 1
        },
        New Drawing.PointF() With {
            .X = 1,
            .Y = 2
        },
        New Drawing.PointF() With {
            .X = 4,
            .Y = 3
        },
         New Drawing.PointF() With {
            .X = 5,
            .Y = 4
        }
    }
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim distinctProduct = product.Distinct(New ProductIdComparer())
            
        End Sub
    
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • 已编辑 Alex-KSGZ 2018年9月30日 8:16
    • 已标记为答案 RF8862 2018年10月1日 3:27
    2018年9月30日 8:15