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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:45:53+00:00 2026-05-11T01:45:53+00:00

Here’s what I’m trying to do. I’m querying an XML file using LINQ to

  • 0

Here’s what I’m trying to do. I’m querying an XML file using LINQ to XML, which gives me an IEnumerable<T> object, where T is my ‘Village’ class, filled with the results of this query. Some results are duplicated, so I would like to perform a Distinct() on the IEnumerable object, like so:

public IEnumerable<Village> GetAllAlliances() {     try     {         IEnumerable<Village> alliances =              from alliance in xmlDoc.Elements('Village')              where alliance.Element('AllianceName').Value != String.Empty              orderby alliance.Element('AllianceName').Value              select new Village              {                  AllianceName = alliance.Element('AllianceName').Value              };          // TODO: make it work...         return alliances.Distinct(new AllianceComparer());     }     catch (Exception ex)     {         throw new Exception('GetAllAlliances', ex);     } } 

As the default comparer would not work for the Village object, I implemented a custom one, as seen here in the AllianceComparer class:

public class AllianceComparer : IEqualityComparer<Village> {     #region IEqualityComparer<Village> Members     bool IEqualityComparer<Village>.Equals(Village x, Village y)     {         // Check whether the compared objects reference the same data.         if (Object.ReferenceEquals(x, y))              return true;          // Check whether any of the compared objects is null.         if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))             return false;          return x.AllianceName == y.AllianceName;     }      int IEqualityComparer<Village>.GetHashCode(Village obj)     {         return obj.GetHashCode();     }     #endregion } 

The Distinct() method doesn’t work, as I have exactly the same number of results with or without it. Another thing, and I don’t know if it’s usually possible, but I cannot step into AllianceComparer.Equals() to see what could be the problem.
I’ve found examples of this on the Internet, but I can’t seem to make my implementation work.

Hopefully, someone here might see what could be wrong here! Thanks in advance!

  • 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. 2026-05-11T01:45:54+00:00Added an answer on May 11, 2026 at 1:45 am

    The problem is with your GetHashCode. You should alter it to return the hash code of AllianceName instead.

    int IEqualityComparer<Village>.GetHashCode(Village obj) {     return obj.AllianceName.GetHashCode(); } 

    The thing is, if Equals returns true, the objects should have the same hash code which is not the case for different Village objects with same AllianceName. Since Distinct works by building a hash table internally, you’ll end up with equal objects that won’t be matched at all due to different hash codes.

    Similarly, to compare two files, if the hash of two files are not the same, you don’t need to check the files themselves at all. They will be different. Otherwise, you’ll continue to check to see if they are really the same or not. That’s exactly what the hash table that Distinct uses behaves.

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

Sidebar

Related Questions

Here's the two scenarios. We are using a manually built xml soap request with
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here is the code in a function I'm trying to revise. This example works
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
Here's some sample HTML <style> .icon {display:none;} </style> <ul> <li>ABC <i id=abc class=icon>x</i></li> <li>DEF
Here is my AsyncTask. Getting exception in the AsyncTask.. private class GetCategories extends AsyncTask<String,
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here my problem: @Assert\Regex( * pattern=/^[A-Za-z0-9][A-Za-z0-9\]*$/, * groups={creation, creation_logged} * ) I'm using the
Here's my Manifest: <?xml version=1.0 encoding=utf-8?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package=com.mappp.mobile android:versionCode=1 android:versionName=1.0 > <supports-screens android:largeScreens=true

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.