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 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 a persistent class Author with two fields: int id , String name
I have a class with like 20 fields which get populated from SQL database
I have a linq2sql class with fields WeekEnding1 WeekEnding2 WeekEnding3 WeekEnding4 I want to
i have class, with fields of double? type. with reflection i get fields Parameters
I have class: static class AnotherClass { int number; String isPrime; public int getNumber()
I have a class with primitive fields like the following: public class Person{ String
I have class with two fields witch are pointers to abstract class. How i
I have a class of string constants, how can I loop through to get
I have a class field char chars[4] I want to init this field in
I have a class with two date fields say: class TestData { Date activation;

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.