Search code examples
c#asp.net.net-corehierarchyid

How to convert HierarchyId to Child/Parent (Tree) structure in C# dotnet?


For some reason such as performance, I have to use HiarachyId in my database. I have to convert the HierarchyId data type to JSON to show up in FancyTree.

I Use the solution here but won't work. My code was

static void Main(string[] args)
    {

        {

        var dd = new List<Field>();
        dd.Add(new Field(1, "Earth", HierarchyId.Parse("/")));
        dd.Add(new Field(2, "Europe", HierarchyId.Parse("/1/")));
        dd.Add(new Field(3, "South America", HierarchyId.Parse("/2/")));
        dd.Add(new Field(4, "Antarctica", HierarchyId.Parse("/3/")));
        dd.Add(new Field(5, "Brazil", HierarchyId.Parse("/2/1/")));
        dd.Add(new Field(6, "France", HierarchyId.Parse("/1/1/")));
        dd.Add(new Field(7, "Germany", HierarchyId.Parse("/1/4/")));
        dd.Add(new Field(8, "test", HierarchyId.Parse("/1/5/")));
        dd.Add(new Field(9, "McMurdo Station", HierarchyId.Parse("/3/1/")));
        dd.Add(new Field(10, "Italy", HierarchyId.Parse("/1/3/")));
        dd.Add(new Field(11, "Spain", HierarchyId.Parse("/1/2/")));
        dd.Add(new Field(12, "Morano", HierarchyId.Parse("/1/3/1/")));
        dd.Add(new Field(13, "Rio de Janeiro", HierarchyId.Parse("/2/1/3/")));
        dd.Add(new Field(14, "Paris", HierarchyId.Parse("/1/1/1/")));
        dd.Add(new Field(15, "Madrid", HierarchyId.Parse("/1/2/1/")));
        dd.Add(new Field(16, "Brasilia", HierarchyId.Parse("/2/1/1/")));
        dd.Add(new Field(17, "Bahia", HierarchyId.Parse("/2/1/2/")));
        dd.Add(new Field(18, "Salvador", HierarchyId.Parse("/2/1/2/1/")));
        dd.Add(new Field(19, "tets1", HierarchyId.Parse("/2/1/3/1/")));
        dd.Add(new Field(20, "test2", HierarchyId.Parse("/2/1/3/1/1/")));
        dd.Add(new Field(21, "test3", HierarchyId.Parse("/2/1/3/1/1/1/")));
        dd.Add(new Field(22, "test24", HierarchyId.Parse("/2/1/3/1/1/2/")));

            MyClass clss = new MyClass();
            var x=  clss.NewMthodTest(dd);

        }

    }

Method to get child:

     public class MyClass
{
    public List<HierarchicalNode> NewMthodTest(List<Field> query)
    {

        var root = new HierarchicalNode("Root", 0);
        foreach (var rec in query)
        {
            var current = root;
            foreach (string part in rec.Node.ToString().Split(new[] { '/' },
                                           StringSplitOptions.RemoveEmptyEntries))
            {
                int parsedPart = int.Parse(part);
                current = current.Children[parsedPart - 1];
            }
            current.Children.Add(new HierarchicalNode(rec.FieldName, rec.Id));
        }
        return null; // in this method i don't know what do we suppose to return 
    }
}

and my input parameter class is :

    public class Field 
    {
        public Field(long id, string fieldName, HierarchyId node)
        {
            Id = id;
            FieldName = fieldName;
            Node = node;
        }
        public long Id { get; set; }
        public string FieldName { get; set; }
        public HierarchyId Node { get; set; }
    }

and output class is

    class HierarchicalNode
{
    private readonly List<HierarchicalNode> children =
        new List<HierarchicalNode>();
    public List<HierarchicalNode> Children { get { return children; } }

    private readonly string name;
    public string Name { get { return name; } }

    private readonly long id;
    public long Id { get { return id; } }

    public HierarchicalNode(string name, long id)
    {
        this.name = name;
        this.id = id;
    }
}

it seems something wrong and it returns this: enter image description here


Solution

  • After a lot of search On the net with no result, I solve this by myself with a recursive method in c#.

    I put my total code here to help people who search in the future for this question. If someone has any nice advice to make it better please leave a note and make me happy.

    This is my code:

    This is My Main method which calls GetTreeMethod

      public List<TreeView> GetTree()
        {
           //get all nodes from DB to make a collection in RAM
            var nodesColect = _getFieldsList.GetFieldsByNode("/");
    
            var x = GetTreeMethod("/", nodesColect);
    
            return x;
        }
    

    This is my main recursive method

    private List<TreeView> GetTreeMethod(string nodeStr,List<FieldListDto> lstCollection)
        {
            List<TreeView> lst = new List<TreeView>();
            HierarchyId node = HierarchyId.Parse(nodeStr);
            var lastItemInCurrentLevel = GetChilds(node, lstCollection);
    
            foreach (var item in lastItemInCurrentLevel)
            {
                TreeView tr = new TreeView
                {
                    title = item.title,
                    id = item.id,
                    node = item.node,
                    fin = item.fin,
                };
                tr.children = GetTreeMethod(item.node.ToString(), lstCollection);
                lst.Add(tr);
            }
    
            return lst;
        }
    

    this just gives children of a specific node

       private List<TreeView> GetChilds(HierarchyId node, List<FieldListDto> lstCollection)
        {
            List<TreeView> child = lstCollection.Where(x => x.Node.ToString() != "/" && x.Node.GetAncestor(1).ToString() == node.ToString()).Select(q => new TreeView { id = q.Id, node = q.Node, title = q.FieldName }).ToList();
            return child;
    
        }
    

    Models

    public class FieldListDto  
    {
        public long id { get; set; }
        public string FieldName { get; set; }
        public HierarchyId Node { get; set; }
    }
     public class TreeView
    {
        public long id { get; set; }
        public string title { get; set; }
        public HierarchyId node { get; set; }
        public List<TreeView> children { get; set; }
    }
    

    here my SQL data enter image description here and here my final result enter image description here