如何解决lower和property的字段匹配,使得结果在DataGridView中成为一行,并且只更新数字。
我的代码有问题吗?请指导我。
我使用的方法有什么问题吗?
谢谢
下面是我使用的代码
Private Sub OnAddRecord(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddProduct.Click
Dim new_product = New Product With {
.Date = dtPicker.Value.ToShortDateString(),
.Code = txtCode.Text,
.Name = txtName.Text,
.Quantity = npQuantity.Value
}
Dim p = list.FirstOrDefault(Function(x) x = new_product)
If p Is Nothing Then
' Product is not found. Add it to list.
list.Add(new_product)
Else
' Product is found. Update quantity.
p.Quantity += new_product.Quantity
End If
dataGrid.Refresh() 'Required to reflect changes
End Sub
Friend Class Product
Implements IEquatable(Of Product)
Public Property [Date]() As String
Public Property Code() As String
Public Property Name() As String
Public Property Quantity() As Decimal
' Overload == operator
Public Shared Operator =(ByVal firstProduct As Product, ByVal secondProduct As Product) As Boolean
' Check for null on left side.
If Object.ReferenceEquals(firstProduct, Nothing) Then
If Object.ReferenceEquals(secondProduct, Nothing) Then
' null == null = true.
Return True
End If
' Only the left side is null.
Return False
End If
' Equals handles case of null on right side.
Return firstProduct.Equals(secondProduct)
End Operator
'Overload != operator (required when overloading == operator)
Public Shared Operator <>(ByVal firstProduct As Product, ByVal secondProduct As Product) As Boolean
Return Not (firstProduct = secondProduct)
End Operator
' 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
而不是比较
Name = other.Name
比较
String.Compare(Name, other.Name, ignoreCase:=True)
整行内容如下:
Return [Date] = other.Date AndAlso Code = other.Code AndAlso String.Compare(Name, other.Name, ignoreCase:=True)