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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:30:17+00:00 2026-05-26T01:30:17+00:00

I have a Node Tree i like to iterate to find all ancestors until

  • 0

I have a Node Tree i like to iterate to find all ancestors until a given point (node) in the tree.
That way i can inserted/save it back to my db.
So far i have something like the following which has been proven very useful, so far:

public IEnumerable<INode> Ancestors()
{
   var parent = this.Parent;
   while (parent != null)
   {
      yield return parent;
      parent = parent.Parent;
   }
}

I think, I should be passing a Func or Func in order to stop/break the sequence.

What would it be the best implementation?.Ta

Edited: Given the answers below. Im thinking going for something more performant like:

 public IEnumerable<INode> Ancestors(Func<INode, bool> predicate)
 {
    var parent = this.Parent;
    while (parent != null)
    {
      if (predicate(parent))
      { 
         yield return parent;
      }
      else
      { 
         yield break; 
      }

      parent = parent.Parent;
    }
 }

Am i right saying Jon’s answer will create 2 enumerators?

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

    How about:

    var desiredNode = child.Ancestors().FirstOrDefault(node => node.Id == desiredId);
    

    That give null if the right node can’t be found.

    EDIT: Okay, if you need the complete sequence from bottom to top, you can just use:

    var nodes = child.Ancestors().TakeUntil(node => node.Id == desiredId);
    

    where TakeUntil is a method like TakeWhile but which includes the final node which matched the predicate. You can find a sample implementation in MoreLINQ. If you don’t mind the lack of argument validation (which MoreLINQ does provide), it’s very simple to write:

    public static IEnumerable<T> TakeUntil<T>(this IEnumerable<T> source,
                                              Func<T, bool> predicate)
    {
        foreach (T item in source)
        {
            yield return item;
            if (predicate(item))
            {
                yield break;
            }
        }
    }
    

    You could build the functionality into the Ancestors() method, but it’s mixing two responsibilities into one relatively-complex function, instead of having two simple functions which can be composed with other simple functions in a very general way.

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

Sidebar

Related Questions

I have an unordered tree. Each node represents a task that can be done
I have a N-Ary non sorted in any way tree and each node can
I have given a tree like this: http://www.seqan.de/dddoc/html/streePreorder.png http://www.seqan.de/dddoc/html/streePreorder.png i can acces each node
I have a binary tree where each node can have a value. I want
I want to have a tree in memory, where each node can have multiple
I have a tree data structure that is L levels deep each node has
I have a binary tree class that is created with a root node and
I have a custom checkbox node tree . The structure is like this .
I have a tree structure that has a node with a parent ID (unlimited
I have a tree in WPF that has no root node, but has defined

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.