ObservableCollection 中的 WPF 唯一条目

发布于 2024-11-07 07:21:51 字数 723 浏览 5 评论 0原文

我有一个 Customers 类的 ObservableCollection,是从数据库查询中填充的。由于表格的连接方式,我有时会得到一次性条目(比尔的送货地址与他的邮寄地址不同),这些条目在数据中显示两次。
每个客户行都有一个 ID 作为唯一主键,当有人从绑定的 ListView 中选择一行时,我用它来提取更多客户信息。 在此程序的 WinForms 版本中,我将在 ListView 中搜索 CustomerID,如果找到,我将避免再次插入它。

ObservableCollection 似乎无法轻松地告诉我 CustomerID 是否已存在于集合的类实例之一中,因此我想知道处理此问题的最佳方法是什么。

到目前为止我的想法:

' Not sure how to make this work, since the CustomerID and Name would be the same, but the city, state, zip might not be.'
t = new classCustomer(CustomerID, CustomerName, City, State, Zip)
if not sr.contains(t) then
  sr.Add(t)
end if

可能弄清楚如何创建 ObservableDictionary,但到目前为止所有示例都是用 C# 编写的,我可能需要一段时间才能将其移植到 VB.net

有人知道更好的实施吗?

I have an ObservableCollection of class Customers that I fill from a database query. Because of how the tables are joined, I sometimes get one off entries (Bill has a shipping address that is different than his mailing address) that show up in the data twice.
Each customer row has an ID as unique primary key, and this is what I use to pull up more customer information when someone selects a row from the bound ListView.
In the WinForms version of this program, I would search the ListView for the CustomerID, and if it was found I would avoid inserting it a second time.

ObservableCollection doesn't seem to have the ability to easily tell me if a CustomerID already exists in one of the class instances of the collection, so I'm wondering what the best way to handle this is.

The ideas I've had so far:

' Not sure how to make this work, since the CustomerID and Name would be the same, but the city, state, zip might not be.'
t = new classCustomer(CustomerID, CustomerName, City, State, Zip)
if not sr.contains(t) then
  sr.Add(t)
end if

Possibly figure out how to create an ObservableDictionary, but so far all the examples are in C#, and it may take me a while to port it over to VB.net

Anyone know of a better implementation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相守太难 2024-11-14 07:21:51

您只需告诉 .NET 一个人的定义是什么,在本例中是 ID。

只需在您的客户对象上覆盖 Equals,那么您的集合现在就能够知道 2 个客户是否相等:

Public Class Person

    Private id As Integer

    Public Sub New(ByVal id As Integer)
        Me.id = id
    End Sub

    Public Overrides Function Equals(ByVal obj As Object) As Boolean

        Return (TypeOf (obj) Is Person) And (DirectCast(obj, Person)).id = Me.id

    End Function

    Public Overrides Function GetHashCode() As Integer
        Return Me.id.GetHashCode()
    End Function

End Class




 Sub Main()

        Dim observable As New ObservableCollection(Of Person)()

        observable.Add(New Person(1))

        Dim duplicate As New Person(1)

        If Not observable.Contains(duplicate) Then
            observable.Add(duplicate) ' never gets hit because of .Equals override
        End If

    End Sub

如果没有覆盖,它不知道如何判断它们是否相等。

You simply need to tell .NET what defines a person, in this case the ID.

Simply override Equals on your customer object, then your collection will now be able to know if 2 customers are equivalent:

Public Class Person

    Private id As Integer

    Public Sub New(ByVal id As Integer)
        Me.id = id
    End Sub

    Public Overrides Function Equals(ByVal obj As Object) As Boolean

        Return (TypeOf (obj) Is Person) And (DirectCast(obj, Person)).id = Me.id

    End Function

    Public Overrides Function GetHashCode() As Integer
        Return Me.id.GetHashCode()
    End Function

End Class




 Sub Main()

        Dim observable As New ObservableCollection(Of Person)()

        observable.Add(New Person(1))

        Dim duplicate As New Person(1)

        If Not observable.Contains(duplicate) Then
            observable.Add(duplicate) ' never gets hit because of .Equals override
        End If

    End Sub

without the override it does not know how to tell if they are equivalent or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文