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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:05:38+00:00 2026-06-02T15:05:38+00:00

So this is the first time I’ve tried using IEqualityComparer and I’m running into

  • 0

So this is the first time I’ve tried using IEqualityComparer and I’m running into an issue.

It’s likely I just don’t understand exactly what the code is doing behind the scenes.

The list I’m providing it looks like this:

Test Run | SN    | Retest
1          185     0
2          185     1
3          185     1
4          185     1

I’m trying to use Distinct() to find the number of items which have a unique SN, and ‘retest==1’.

var result = testRunList.Distinct(new UniqueRetests());

And the derived IEqualityCompare class looks like this:

public class UniqueRetests : IEqualityComparer<TestRunRecord>
{
    // Records are equal if their SNs and retest are equal.
    public bool Equals(TestRunRecord x, TestRunRecord y)
    {
        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether it's a retest AND if the specified records' properties are equal.
        return x.retest == 1 && y.retest == 1 && x.boardSN == y.boardSN;
    }

    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.

    public int GetHashCode(TestRunRecord record)
    {
        //Check whether the object is null
        if (Object.ReferenceEquals(record, null)) return 0;

        //Get hash code for the board SN field.
        int hashRecordSN = record.boardSN.GetHashCode();

        //Get hash code for the retest field.
        int hashRecordRetest = record.retest.GetHashCode();

        //Calculate the hash code for the product.
        return hashRecordSN ^ hashRecordRetest;
    }
}

Problem is that this seems to produce a which includes the first two items, whereas what I’m looking for is a list that includes only a single item where ‘retest==1’.

Any idea what I’m doing wrong here? How is it that a record with ‘retest == 0’ is being returned?

Answer

If the condition is false, the objects are treated as if they are not equal. Distinct returns not-equal rows. Btw, you are violating the contract of IEqualityComparer with this type of code. The results are actually undefined. – usr

With "violating the contract" I mean (for example) than an object with retest==0 will compare unequal to itself. – usr

  • 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-02T15:05:39+00:00Added an answer on June 2, 2026 at 3:05 pm

    You need to filter out items with retest = 0. Put a .Where(x => x.retest != 0) in front of the Distinct.

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

Sidebar

Related Questions

This is the first time ever I'm using AJAX, and I want to do
this is my first time using StAX for parsing XML documents (still in the
I am doing this first time so I need some help. I am using
I'm using PayPal sandbox for the payment gateway, this is first time I'm trying
this is my first time posting here, I have a question which I have
This is my first time on StackOverflow though I read Coding Horror quite often.
This is the first time I'm creating an open-source project, and I've decided (based
This is the first time I'm trying random numbers with C (I miss C#).
This is my first time making a hash table. I'm trying to associate strings
This is my first time here so I hope I post this question at

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.