如何在 vb.net 中将 IEquatable<T> 与字段匹配正确和更低

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

提问人:dlaksmi 提问时间:9/12/2023 最后编辑:dlaksmi 更新时间:9/13/2023 访问量:53

问:

如何解决下级和正确之间的字段匹配,以便结果在 DataGridView 中成为一行,并且只更新数字。

我的代码有问题吗?请指导我。

我使用的方法有什么问题吗?

谢谢

下面是我使用的代码

        ' 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

vb.net LINQ 源绑定 列表 IEquatable

评论

0赞 jmcilhinney 9/12/2023
您的问题基本上是如何对两个字符串进行不区分大小写的比较,很明显您实际上并没有在网络上搜索过它。在这里发布问题之前,您需要使用搜索引擎。
0赞 dlaksmi 9/12/2023
@jmcilhinney,请指导我
0赞 dbasnett 9/13/2023
不知道这是否适用于此处,但“如果实现 IEquatable<T>,则还应该实现 IComparable<T>如果可以对类型的实例进行排序或排序。

答:

1赞 Olivier Jacot-Descombes 9/12/2023 #1

而不是比较

Name = other.Name

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

然后整行内容如下:

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

这不是唯一的可能性。另请参阅:在 .NET 中比较字符串的最佳做法(如果页面显示为 C#,则可以切换到页面右上角的 VB)。

或者,正如@jmcilhinney指出的那样,您可以使用

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

您可以在 和 之间进行选择。 这将返回一个 .StringComparison.CurrentCultureIgnoreCaseStringComparison.InvariantCultureIgnoreCaseStringComparison.OrdinalIgnoreCaseBoolean

评论

0赞 dlaksmi 9/12/2023
,感谢您的回复,但我尝试使用您的答案,我尝试添加按钮,但它出现了几行Return [Date] = other.Date AndAlso Code = other.Code AndAlso CBool(String.Compare(Name, other.Name, ignoreCase:=True))
0赞 Olivier Jacot-Descombes 9/12/2023
哦,我错过了一个细节。 返回一个 Integer。所以它一定是.这将返回,如果您搜索 .String.CompareString.Compare(Name, other.Name, ignoreCase:=True) = 0"name 1""Name 1""name"
0赞 dlaksmi 9/12/2023
你的回答很完美。Return [Date] = other.Date AndAlso Code = other.Code AndAlso CBool(String.Compare(Name, other.Name, ignoreCase:=CInt(True) = 0))
0赞 Olivier Jacot-Descombes 9/12/2023
CBool不是必需的,因为返回 a 已经。String.Compare(...) = 0Boolean
1赞 jmcilhinney 9/12/2023
使用 ,它也可以忽略大小写并且已经返回 . 应该用于相对排序。String.EqualsBooleanString.Compare