提问人:dlaksmi 提问时间:9/12/2023 最后编辑:dlaksmi 更新时间:9/13/2023 访问量:53
如何在 vb.net 中将 IEquatable<T> 与字段匹配正确和更低
how to use IEquatable<T> with field match Proper and lower in vb.net
问:
如何解决下级和正确之间的字段匹配,以便结果在 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
答:
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.CurrentCultureIgnoreCase
StringComparison.InvariantCultureIgnoreCase
StringComparison.OrdinalIgnoreCase
Boolean
评论
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.Compare
String.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(...) = 0
Boolean
1赞
jmcilhinney
9/12/2023
使用 ,它也可以忽略大小写并且已经返回 . 应该用于相对排序。String.Equals
Boolean
String.Compare
评论