简体   繁体   中英

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?

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" };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM