vb.netlinqbindingsourcebindinglistiequatable

how to use IEquatable<T> with field match Proper and lower in vb.net


How to solve the field match between lower and proper so that the results become one row in the DataGridView and only update the number.

Is there something wrong with my code? and please guide me.

is there anything wrong the method i use?

Thanks

below is the code I used

        ' Implementing IEquatable<T> interface
        Public Overloads Function Equals(ByVal other As Product) As Boolean Implements IEquatable(Of Product).Equals
            ' If 'other' is null, return false.
            If Object.ReferenceEquals(other, Nothing) Then
                Return False
            End If

            ' Optimization for a common success case.
            If Object.ReferenceEquals(Me, other) Then
                Return True
            End If

            ' If run-time types are not exactly the same, return false.
            If Me.GetType() IsNot other.GetType() Then
                Return False
            End If

            ' Return true if the fields match.
            Return [Date] = other.Date AndAlso Code = other.Code AndAlso Name = other.Name
        End Function
    End Class

Screenshot_2023_09_12-2


Solution

  • Instead of comparing

    Name = other.Name
    

    compare with

    String.Compare(Name, other.Name, ignoreCase:=True) = 0
    

    The whole line then reads:

    Return [Date] = other.Date AndAlso Code = other.Code AndAlso String.Compare(Name, other.Name, ignoreCase:=True) = 0
    

    This is not the only possibility. See also: Best practices for comparing strings in .NET (if the page displays with C#, you can switch to VB at the top-right of the page).

    Alternatively, as @jmcilhinney pointed out, you can use

    Name.Equals(other.Name, StringComparison.CurrentCultureIgnoreCase)
    

    where you have the choice between StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCultureIgnoreCase and StringComparison.OrdinalIgnoreCase. This returns a Boolean.