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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:49:05+00:00 2026-05-30T05:49:05+00:00

when passing EqualityComparer as last parameter to Linq Join method it is not using

  • 0

when passing EqualityComparer as last parameter to Linq Join method it is not using Equals method of it, it for some reason is using GetHashCode to compare items.

Is it possible to make it use Equals instead?

        var ss = new string[] { "aa", "bb", "cc" };
        var zz = new string[] { "aa", "zz", "cc" };

        var res = ss
            .Join(zz, 
                o => o, 
                i => i, 
                (i, o) => i + o, 
                new GenericEqualityComparer<String>((x,y) => x == y))
            .ToList();
  • 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-30T05:49:08+00:00Added an answer on May 30, 2026 at 5:49 am

    When an IEqualityComparer<T> compares to objects, it first compares their hashcodes. Only if they are equal the Equals method is used to refine the comparison. So in your case it should at least hit Equals twice.

    To demonstrate what an EqualityComparer does I made a little code snippet in Linqpad:

    void Main()
    {
        var ss = new string[] { "aa1", "bb1", "cc1" };
        var zz = new string[] { "aa2", "aa3", "zz2", "cc2" };
    
        var res = ss.Join(zz,  o => o, i => i, (i, o) => i + o,
            new SubstringComparer()).ToList();
    }
    
    public class SubstringComparer : IEqualityComparer<string>
    {
        public bool Equals(string left, string right)
        {
            string.Format("{0} - {1}", left, right).Dump();
            return left.Substring(0,2) == right.Substring(0,2);
        }
    
        public int GetHashCode(string value)
        {
            value.Dump();
            return value.Substring(0,2).GetHashCode();
        }
    }
    

    So strings are equal if their first two characters are equal. The output is:

    aa2
    aa3
    aa2 – aa3
    zz2
    cc2
    aa1
    aa2 – aa1
    bb1
    cc1
    cc2 – cc1

    And the resulting list:

    aa1aa2
    aa1aa3
    cc1cc2

    You see that first the second list is compared (I’m not sure why, by the way, maybe the hashcodes are cached) and then the pairs.

    So when your GenericEqualityComparer never hits Equals it somehow always generates a unique hashcode, which I think should be a bug. If it not always uses Equals, here is the explanation. And if you want a comparer to always use Equals you should make it always return an identical hashcode (which is inefficient, of course).

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

Sidebar

Related Questions

Passing Parameter to the Java file using Html form I couldn't run in my
passing post data using cURL requires that the name of the input. However, I
Passing in an Expression to a Linq query behaves differently depending on syntax used,
When you're passing variables through your site using GET requests, do you validate (regular
If I am passing an object to a method, why should I use the
I've recently started using LINQ quite a bit, and I haven't really seen any
I'm passing a map of strings from native c++ class to c# using c++/CLI.
I'm passing as parameter an id to a javascript function, because it comes from
Parameter passing in Visual Studio . Note how __m128 types are passed. Does it
Passing beginning date and final date as parameter through the following code as no

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.