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

  • Home
  • SEARCH
  • 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 3350822
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:50:01+00:00 2026-05-18T01:50:01+00:00

Been trying to solve this without progress for a couple days, my question: Given

  • 0

Been trying to solve this without progress for a couple days, my question:

Given an entity in a hierarchy, return
a list of the immediate-ascendants (or
parents) of the entity.

When I want to know the hierarchy of “Entity 1.2.2.2” it would return a list containing only the bold items:

Entity 1
- Entity 1.1
- Entity 1.2
  - Entity 1.2.1
  - Entity 1.2.2
    - Entity 1.2.2.1
    - Entity 1.2.2.2
  - Entity 1.2.3
  - Entity 1.2.4
    - Entity 1.2.4.1
- Entity 1.3
- Entity 1.4
Entity 2
...

Hence, expected result:

Entity 1
- Entity 1.2
  - Entity 1.2.2
    - Entity 1.2.2.2

Implementation code:

class Entity
{
    public Entity Parent { get; set; }

    public bool AbsoluteParent 
    { 
        get
        { 
            return Parent == null;
        } 
    }

    public IEnumerable<Entity> Hierarchy //problem
    { 
        get
        {
            return AbsoluteParent 
                ? new [] { this }
                : new [] { this }.Concat( Parent.Hierarchy );
        }
    }

}

The array attempt above is just one of the options I have been trying, that code actually returns something like:

Entity 1
Entity 1
- Entity 1.2
Entity 1
- Entity 1.2
  - Entity 1.2.2
Entity 1
- Entity 1.2
  - Entity 1.2.2
    - Entity 1.2.2.2

I am able to achieve the expected results using jQuery’s parents() function, I’ve been reading the yield return keywords but still stuck in its more (I guess) functional style which I’m still baby-stepping into.

  • 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-18T01:50:02+00:00Added an answer on May 18, 2026 at 1:50 am

    Not a very LINQ way of doing it (my LINQ is still pretty new):

    List<Entity> graph = new List<Entity>()
    Entity current = this;
    
    while (current != null) {
       graph.Add(current);
       current = current.Parent;
    }
    
    graph.Reverse();
    
    return graph;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a problem I've been trying to solve at work. I'm not a database
Been trying to find a working implementation of a WPF listview (or listbox) where
Been trying to upgrade my subversion installation, but due to (what I believe) are
I been trying to get sqlcachedependecy working, but it doesn't appear to work I
I've been trying to understand how Ruby blocks work, and to do that I've
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
We've been trying to alter a lot of columns from nullable to not nullable,
I've been trying to use SQLite with the PDO wrapper in PHP with mixed
I have been trying to find a really fast way to parse yyyy-mm-dd [hh:mm:ss]
I've been trying to wrap my head around how threads work in Python, and

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.