简体   繁体   English

从数据库创建树

[英]Creating tree from the db

I have DataTable consists of rows which have fields called recordId,recordName,parentId. 我的DataTable由行组成,这些行具有称为recordId,recordName,parentId的字段。 What I want to do is to show parent children relation of the records but I cannot create a concrete logic on how to build such a tree. 我想要做的是显示记录的父子关系,但是我无法就如何构建这样的树创建具体的逻辑。 I tried to get the records into a model like below: 我试图将记录放入如下模型中:

public class Records
{
    public int recordId{ get; set; }
    public string recordName{ get; set; }
    public List<Records> ChildRecords{ get; set; }

}

Then I started with the records which have parentId==0(These are all the children of the root). 然后,我从具有parentId == 0(这些都是根的所有子代)的记录开始。 Then I recursively tried to get the child records of those, however, I stuck at some points several times. 然后,我递归地尝试获取这些记录的子记录,但是,我在某些时候停留了好几次。

Is there anyone who can show me a way to create that family tree. 有谁可以向我展示一种创建家谱的方法。

Edit: 编辑:

lets say DB entry: (1 A 0,2 B 0,3 C 1, 4 D 1, 5 E 3,6 F 2,7 G 6,8 H 7) 可以说DB条目:(1 A 0,2 B 0,3 C 1,4 D 1,5 E 3,6 F 2,7 G 6,8 H 7)

A -> (C->E,D) A->(C-> E,D)

B ->(F->G->H) B->(F-> G-> H)

What I tired is basically getting the children of the root. 我所累的基本上是让孩子扎根。 Then based on the acquired id I got to the last children of the parent. 然后根据获取的ID,我找到了父母的最后一个孩子。

Ex: for A first I got A and then I went through C, then got its child E. After that I tried to keep track of where to put D and messed up. 例如:对于A,我首先得到A,然后经历C,然后得到其子E。之后,我试图跟踪放置D的位置并弄糟。

namespace TestConsoleApp
{
    public class Record
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int? ParentId { get; set; }
        public List<Record> ChildRecords { get; set; }

    }

    class Program
    {
        static void Main(string[] args)
        {
            var RecordsStore = new List<Record>();

            //Get the standalone recoreds
            var standaloneRecords = RecordsStore.Where(x => x.ParentId == null).Select(x => new Record
            {
                Id = x.Id,
                Name = x.Name,
                ChildRecords = new List<Record>()
            });

            //Traverse
            List<Record> records = new List<Record>();
            foreach (var item in standaloneRecords)
                records.Add(Traverse(item, RecordsStore));

        }


        private static Record Traverse(Record parent, List<Record> records)
        {
            parent.ChildRecords = records.Where(x => x.ParentId == parent.Id)
                                              .Select(x => new Record
                                              {
                                                  Id = x.Id,
                                                  Name = x.Name,
                                                  ChildRecords = new List<Record>()
                                              }).ToList();
            foreach (var item in parent.ChildRecords)
                Traverse(item, records);
            return parent;
        }
    }  
}

You can do something like this 你可以做这样的事情

You can declare your model class like this 您可以这样声明模型类

    public class Record
    {
        [Key]
        public int recordId{ get; set; }
        public string recordName{ get; set; }

        [ForeignKey("ParentRecordDetails")]
        public int ParentId {get;set;}
        public Record ParentRecordDetails {get;set;}

    }

and then write self join in LINQ to recursively get tree structure 然后在LINQ中编写自联接以递归获取树结构

Reference: https://bitlush.com/blog/recursive-hierarchical-joins-in-c-sharp-and-linq 参考: https : //bitlush.com/blog/recursive-hierarchical-joins-in-c-sharp-and-linq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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