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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:46:36+00:00 2026-06-10T03:46:36+00:00

I am trying to get a distinct list of elements but it always returns

  • 0

I am trying to get a distinct list of elements but it always returns everything.

Xml:

<root>
    <row>
        <unit>CAN</unit>
    </row>
    <row>
        <unit>KG</unit>
    </row>
    <row>
        <unit>KG</unit>
    </row>
    <row>
        <unit>PKT</unit>
    </row>
    <row>
        <unit>CAN</unit>
    </row>
    <row>
        <unit>PKT</unit>
    </row>
    <row>
        <unit>KG</unit>
    </row>
</root>

Linq:

List<XElement> elements = (from e in xdoc.Descendants("row").Elements()
     where e.Name.Equals("unit")
     select e).Distinct().ToList();

Expected output:
elements list should contain 3 items

    <unit>CAN</unit>
    <unit>KG</unit>
    <unit>PKT</unit>
  • 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-06-10T03:46:37+00:00Added an answer on June 10, 2026 at 3:46 am

    Distinct() does not know what part of the XElement to compare, so it will just compare the object reference. Since all XElements are separate objects, no matter the content, it will return them all.

    You either have to define an IEqualityComparer that allows it to compare them (probably the preferred way to do it) or just use GroupBy that can take a lambda that will allow it to compare the actual Value instead;

    var elements = (from e in xdoc.Root.Elements("row").Elements("unit")
                    select e).GroupBy(x => x.Value).Select(x => x.First());
    

    Output:

    <unit>CAN</unit>
    <unit>KG</unit>
    <unit>PKT</unit>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get distinct id's out of a 6 million row table. The
I'm trying to get a distinct list of characters (case sensitive) across all strings
Im trying to query a List but unable to get the result the way
I am trying to get distinct FullNames from a list that contains FullNames and
I'm trying to get a distinct list of words from an array of words
I am trying to get a distinct list of one particular column. Somehow or
I am trying to get the list of distinct store_names in the list of
I have a problem trying to get a Distinct List of my Class Objects.
I have a 3 tables that I'm trying to join and get distinct results.
Trying to get this expression to work, can someone look at it and tell

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.