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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:13:08+00:00 2026-05-19T14:13:08+00:00

Not to repeat this question too much, but I already did a search and

  • 0

Not to repeat this question too much, but I already did a search and came up empty on a result. So I have two EntityCollections of type T and I would like to find the common items in each. The catch? All fields except one must match. So for example, if type T is a type CustomSet, and CustomSet includes fields F1, F2, F3 and a FK field OtherId, F1, F2 and F3 must match (they could be strings, ints, anything really) and the OtherId will never match. My current implementation:

var intersections = source.Intersect(destination).ToList();

will never yield any results because that OtherId column will never match in any other collection, even though the fields F1, F2 and F3 may match. So I’m proposing a custom implementation of IEqualityComparer which looks like this:

var intersections = source.Intersect(destination, new EntityCollectionComparer<T>()).ToList();

public class EntityCollectionComparer<T> : IEqualityComparer<T>
{
    #region IEqualityComparer<T> Members

    public bool Equals(T x, T y)
    {
        if (x.Equals(y))
            return true;
        else
            return false;
    }

    public int GetHashCode(T obj)
    {
        if (obj is CustomSet)
        {
            CustomSet temp = obj as CustomSet;

            return (temp.F1.GetHashCode() ^ temp.F2.GetHashCode() ^ temp.F3.GetHashCode());
        }
        return obj.GetHashCode();
    }

Now, I’m only testing this so the obj getting passed in is of type CustomSet, I will add the necessary if statements for my other types if I can get this to function properly. I know that the Intersect extension uses the GetHashCode instead of the Equals to compare items which is why I really don’t care what’s in my equals as this class will never be called but for the Intersect extension on EntityCollections. The thing is, this doesn’t work. On my test set, I know I have 28 items in my ‘source’ collection, and 28 items in my ‘destination’ collection, and all the fields match (obviously except for the OtherId field). I stepped through the GetHashCode code as it looped 56 times and was able to match up hash codes on all 28 items from each set yet the ‘intersections’ yielded 0 count. Is there something I’m doing wrong, or missing? Thanks.
}

  • 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-19T14:13:08+00:00Added an answer on May 19, 2026 at 2:13 pm

    This is your problem:

    I know that the Intersect extension uses the GetHashCode instead of the Equals to compare items which is why I really don’t care what’s in my equals as this class will never be called but for the Intersect extension on EntityCollections.

    That’s simply not true. GetHashCode is used as a first “quick” way of bucketing values, but Equals will still be called for any items with the same hash, otherwise you can’t know they’re equal.

    That’s the way hash tables etc always work: the hashes should be different for unequal values where feasible for performance reasons, but are allowed to collide.

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

Sidebar

Related Questions

Forgive me if this is a repeat question. I've searched StackOverflow and did not
I'm not too good with css so this is probably a stupid question but...
This question might seem a repeat of previous ones. I have read through a
No, this is not a repeat of this question ... a little related though,
This question must be a repeat since it sounds obvious but I cannot seem
I want to repeat this question using python. Reason is I have access to
I know this is similar to my other question but I made it too
Sorry if this is a repeat question. I've looked at other threads, but while
I'm trying to not repeat code, so I have a method that does a
Not any homework, but seem to have got lost while doing basics, hence asking.

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.