Search code examples
c#enumerable

How to deal with Parent/Child/GrandChild relationship?


I am looking for a way to represent an object that has a Parent, a Child, and a grandchild object. I do not want to use:

 IEnumerable<IEnumerable<IEnumerable<Node>>>

If it is at all possible.

Each Node is the same:

public class Node
{
    public string Text { get; set; }
    public string Value { get; set; }
    public string Title { get; set; }
}

I need to represent a tree like structure where there are three levels of data. Ex

 ParentNode
     ChildNode

 ParentNode
     ChildNode
         GrandChildNode
         GrandChildNode

I am trying to do this as generically / clean as possible so that I can reuse the service that gets this information from the database.

Any Sugestions?


Solution

  • You can modify your class to accommodate a tree like hierarchy.

    public class Node
    {
        public string Text { get; set; }
        public string Value { get; set; }
        public string Title { get; set; }
    
        public Node Parent { get; private set; }
        public ICollection<Node> Children { get; private set; }
    
        public IEnumerable<Node> Ancestors() {
            Node current = this.Parent;
            while (current != null) {
                yield return current;
                current = current.Parent;                
            }
        }
    
        public IEnumerable<Node> Descendants() {
            foreach (Node c in this.Children) {
                yield return c;
                foreach (Node d in c.Descendants())
                    yield return d;
            }
        }
    
        // Root node constructor
        public Node() {
            this.Children = new List<Node>();     
        }
    
        // Child node constructor
        public Node(Node parent) : this() {
            this.Parent = parent;
            parent.Children.Add(this);
        }
    }
    

    You can then use it like so:

    Node gramps = new Node() { Title = "Grandparent" };
    Node dad = new Node(gramps) { Title = "Parent" };
    Node son = new Node(dad) { Title = "Child" };