如何在C#dotnet中将HierarchyId转换为子/父(树)结构? [英] How to convert HierarchyId to Child/Parent (Tree) structure in C# dotnet?

查看:87
本文介绍了如何在C#dotnet中将HierarchyId转换为子/父(树)结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因(例如性能),我必须在数据库中使用HiarachyId。我必须将HierarchyId数据类型转换为JSON才能显示在FancyTree中。



我使用解决方案

解决方案

经过大量搜索后,网上没有结果,我用c#中的递归方法自己解决了这个问题。



我将全部代码放在这里,以帮助以后搜索此问题的人。如果有人有更好的建议,请改善我的生活。



这是我的代码:



这是我的主要方法,调用GetTreeMethod

  public List< TreeView> GetTree()
{
//从数据库中获取所有节点以在RAM中收集
var nodesColect = _getFieldsList.GetFieldsByNode( /);

var x = GetTreeMethod( /,nodesColect);

返回x;
}

这是我的主要递归方法

 私有列表< TreeView> GetTreeMethod(字符串nodeStr,List< FieldListDto> lstCollection)
{
List< TreeView> lst = new List< TreeView>();
HierarchyId节点= HierarchyId.Parse(nodeStr);
var lastItemInCurrentLevel = GetChilds(node,lstCollection);

foreach(lastItemInCurrentLevel中的可变项)
{
TreeView tr =新的TreeView
{
title = item.title,
id = item.id,
节点= item.node,
fin = item.fin,
};
tr.children = GetTreeMethod(item.node.ToString(),lstCollection);
lst.Add(tr);
}

返回lst;
}

这只是给一个特定节点的子对象

 私人列表< TreeView> GetChilds(HierarchyId节点,List< FieldListDto> lstCollection)
{
List< TreeView> child = lstCollection.Where(x => x.Node.ToString()!= /&& x.Node.GetAncestor(1).ToString()== node.ToString())。Select(q =>新的TreeView {id = q.Id,node = q.Node,title = q.FieldName})。ToList();
个回返孩子;

}

模型

  public class FieldListDto 
{
public long id {get;组; }
公用字串FieldName {get;组; }
public HierarchyId节点{组; }
}
公共类TreeView
{
public long id {get;组; }
公用字符串标题{get;组; }
公共HierarchyId节点{get;组; }
public List< TreeView>孩子们{组; }
}

此处是我的SQL数据

,这里是我的最终结果


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:

解决方案

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 and here my final result

这篇关于如何在C#dotnet中将HierarchyId转换为子/父(树)结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆