Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5849597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:01:22+00:00 2026-05-22T13:01:22+00:00

I have an ObservableCollection of class Customers that I fill from a database query.

  • 0

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?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T13:01:23+00:00Added an answer on May 22, 2026 at 1:01 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that inherits from ObservableCollection(Of MyObject) and the MyObject class handles
I have the following code: class Customers : BindableObject { private ObservableCollection<string> _Products =
I have an ObservableCollection that I can't seem to get to display in a
I have list with items in my data class: public ObservableCollection<Node> NodesFromDatabase { get
I have a collection similar to: Public Class MyCollection Inherits ObservableCollection(Of MyCollection) Private _Name
I have a collection private ObservableCollection<Contact> _contacts; In the constructor of my class I
I have a ViewModel defined like this: public class LocationTreeViewModel<TTree> : ObservableCollection<TTree>, INotifyPropertyChanged TTree
Well, I have the following class definitions: private ObservableCollection<Node> _Nodes; public ObservableCollection<Node> Nodes {
I have an ObservableCollection of addresses that I am binding to a ListBox. Then
I have ObservableCollection<Foo> that is bound to an ItemsControl (basically displaying a list). Foo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.