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

  • Home
  • SEARCH
  • 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 479503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:47:56+00:00 2026-05-13T00:47:56+00:00

I have a class with fields ColDescriptionOne(string), ColDescriptionTwo(string) and ColCodelist(int). I want to get

  • 0

I have a class with fields ColDescriptionOne(string), ColDescriptionTwo(string) and ColCodelist(int). I want to get the Intersect of two lists of this class where the desc are equal but the codelist is different.

I can use the Where clause and get what I need. However I can’t seem to make it work using a custom Comparer like this:

internal class CodeListComparer: EqualityComparer<SheetRow>
{
    public override bool Equals(SheetRow x, SheetRow y)
    {
        return Equals(x.ColDescriptionOne, y.ColDescriptionOne) &&
               Equals(x.ColDescriptionSecond, y.ColDescriptionOne)
               && !Equals(x.ColCodelist, y.ColCodelist);
    }

    public override int GetHashCode(SheetRow obj)
    {
        return ((obj.ColDescriptionOne.GetHashCode()*397) + (obj.ColDescriptionSecond.GetHashCode()*397)
                + obj.ColCodelist.GetHashCode());            
    }
}

And then use it like this:

var onylByCodeList = firstSheet.Entries.Intersect(otherSheet.Entries, new CodeListComparer());

Any ideas what I’m doing wrong here ?

thanks
Sunit

  • 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-05-13T00:47:57+00:00Added an answer on May 13, 2026 at 12:47 am

    You have a typo in the Equals method. The second line is comparing ColDescriptionOne to ColDescriptionSecond. They should both be ColDescriptionSecond.

    return Equals(x.ColDescriptionOne, y.ColDescriptionOne) 
           && Equals(x.ColDescriptionSecond, y.ColDescriptionSecond)
           && !Equals(x.ColCodelist, y.ColCodelist);
    

    The second problem you have is you are including the ColCodeList in the GetHashCode method. The GetHashCode method must return the same value for objects that are equal. In this case though ColCodeList is supposed to be different when values are equal. This means that in cases where you want 2 objects to be considered equal they are more likely to have different hash codes which is incorrect.

    Take that out of the GetHashCode method and everything should work.

    public override int GetHashCode(SheetRow obj)
    {
        return ((obj.ColDescriptionOne.GetHashCode()*397) 
               + (obj.ColDescriptionSecond.GetHashCode()*397));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have class, with fields of double? type. with reflection i get fields Parameters
I have a Java class with ~90 fields. I want to be able to
I have some class with lots of fields; public class CrowdedHouse { public int
I have class with two fields witch are pointers to abstract class. How i
I have a class made up of several fields, and I have several constructors.
i have a class whose equality is based on 2 fields such that if
I have a field object and I create a list of fields: class Field
Let's say you have a class called Customer, which contains the following fields: UserName
I have an immutable class with some private fields that are set during the
I have a domain class containing a couple of fields. I can access them

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.