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 8885601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:20:57+00:00 2026-06-14T21:20:57+00:00

I have some code with two lists of objects. The first list is more

  • 0

I have some code with two lists of objects. The first list is more inclusive than the second list. I wish to exclude items in the second list from the first list. After some research I found that the extension method Except is the way to do this. I therefor implemented IEquatable(Of T) and IEqualityComparer(Of T) in my code for something like this:

Partial Public Class CustomerData
    Implements IEquatable(Of CustomerData)
    Implements IEqualityComparer(Of CustomerData)

    Public Overloads Function Equals(other As CustomerData) As Boolean Implements IEquatable(Of ToolData.CustomerData).Equals
        If other Is Nothing Then
            Return False
        Else
            Return Me.CustomerID = other.CustomerID
        End If
    End Function

    Public Overloads Function Equals(this As CustomerData, that As CustomerData) As Boolean Implements IEqualityComparer(Of ToolData.CustomerData).Equals
        If this Is Nothing OrElse that Is Nothing Then
            Return False
        Else
            Return this.CustomerID = that.CustomerID
        End If
    End Function

    Public Overloads Function GetHashCode(other As CustomerData) As Integer Implements IEqualityComparer(Of ToolData.CustomerData).GetHashCode
        If other Is Nothing Then
            Return CType(0, Integer).GetHashCode
        Else
            Return other.CustomerID.GetHashCode
        End If
    End Function

I then make a simple call like such:

customerCollection = CustomerData.LocalCustomers.Except(CustomerData.RecentCustomers).OrderBy(Function(x) x.FullName).ToList

This doesn’t work. Neither does this:

customerCollection = CustomerData.LocalCustomers.Except(CustomerData.RecentCustomers, EqualityComparer(Of CustomerData).Default).OrderBy(Function(x) x.FullName).ToList

However, this does work:

customerCollection = CustomerData.LocalCustomers.Except(CustomerData.RecentCustomers, New CustomerData).OrderBy(Function(x) x.FullName).ToList

Since RecentCustomers and LocalCustomers are both List(Of CustomerData) why wouldn’t the default compare method work? When I say it doesn’t work I mean that I can put break points in the Equals and GetHashCode routines and they are never hit and the list returned is identical to the list of LocalCustomers.

  • 1 1 Answer
  • 0 Views
  • 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-06-14T21:20:58+00:00Added an answer on June 14, 2026 at 9:20 pm

    First, you don’t need to implement the IEqualityComparer(Of T) interface; you would normally implement that in another class if you wanted to provide multiple types of equality for the same class.

    Second, you’ll need to override the GetHashCode and Equals(Object) methods:

    Partial Public Class CustomerData
       Implements IEquatable(Of CustomerData)
    
       Public Override Function GetHashCode() As Integer
          Return CustomerID.GetHashCode()
       End Function
    
       Public Override Function Equals(ByVal obj As Object)
          Return Equals(TryCast(obj, CustomerData))
       End Function
    
       Public Overloads Function Equals(other As CustomerData) As Boolean Implements IEquatable(Of ToolData.CustomerData).Equals
          If other Is Nothing Then
             Return False
          Else
             Return Me.CustomerID = other.CustomerID
          End If
       End Function
    
       ...
    

    Here’s a blog post which explains why:
    http://blogs.msdn.com/b/jaredpar/archive/2009/01/15/if-you-implement-iequatable-t-you-still-must-override-object-s-equals-and-gethashcode.aspx

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

Sidebar

Related Questions

I have two objects - one that contains some code with will fire an
I have some code on two systems running kernel 2.4.20 and kernel 2.4.38 .
I have some existing code which isn't formatted consistently -- sometimes two spaces are
I have inherited some HTML code and have been asked to align the two
I have a drop down list and some styles applied to it from two
I have two listviews, clicking an item on the first list view loads the
I have two drop down lists; the first is bound to an entity datasource,
I have some code that will change the background color of a specific label
I have some code here that uses bitsets to store many 1 bit values
I have some code that is supposed to return an NSString. Instead it is

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.