按级别

时间:2018-06-19 01:34:02

标签: c# tree

我有这个表按级别排序

name    parentId    id  level
Dep 1   0           1    0  
Dep 2   1           2    1  
Dep 6   2           6    2  
Dep 4   1           4    1  
Dep 5   4           5    2
Dep 3   0           3    0  

获得父母所有孩子的最佳方法是什么?我想过使用一个字典来存储所有id,然后创建一个递归函数来搜索每个父节点的所有子节点。但由于它已经按级别排序,我真的需要一个递归函数,或者我可以以迭代的方式进行吗?

结果预期:

[  
   {
     "id"   : "1", 
     "name" : "Dep1",
     "children": [
        {
           "id"  : "2", 
           "name" : "Dep2",
           "children": [
              {
               "id"  : "6", 
               "name" : "Dep6"
              }
           ]
        }
      ]
     "id"   : "4", 
     "name" : "Dep4",
     "children": [
        {
           "id"  : "5", 
           "name" : "Dep5"
        }
     ]
     {
     "id"   : "3", 
     "name" : "Dep3"
     }
  }
]

2 个答案:

答案 0 :(得分:1)

我会采用这种方法:

List<Data> data = new List<Data>()
{
    new Data() { Name = "Dep 1", ParentId = 0, Id = 1, Level = 0 },
    new Data() { Name = "Dep 2", ParentId = 1, Id = 2, Level = 1 },
    new Data() { Name = "Dep 6", ParentId = 2, Id = 6, Level = 2 },
    new Data() { Name = "Dep 4", ParentId = 1, Id = 4, Level = 1 },
    new Data() { Name = "Dep 5", ParentId = 4, Id = 5, Level = 2 },
    new Data() { Name = "Dep 3", ParentId = 0, Id = 3, Level = 0 },
};

var lookup = data.ToLookup(x => x.ParentId);

List<Result> GetChildren(int parentId) =>
    lookup[parentId]
        .Select(c => new Result()
        {
            Id = c.Id,
            Name = c.Name,
            Children = GetChildren(c.Id)
        })
        .ToList();

List<Result> result = GetChildren(0);

这给出了:

[
  {
    "Id": 1,
    "Name": "Dep 1",
    "Children": [
      {
        "Id": 2,
        "Name": "Dep 2",
        "Children": [
          {
            "Id": 6,
            "Name": "Dep 6",
            "Children": []
          }
        ]
      },
      {
        "Id": 4,
        "Name": "Dep 4",
        "Children": [
          {
            "Id": 5,
            "Name": "Dep 5",
            "Children": []
          }
        ]
      }
    ]
  },
  {
    "Id": 3,
    "Name": "Dep 3",
    "Children": []
  }
]

我同意这需要在大型数据集上进行测试,以确定它是否有内存或计算限制。

答案 1 :(得分:0)

以下解决方案适合您。您需要安装NewtonSoft.Json nuget包以将对象序列化为json。

class Program
{
    static void Main(string[] args)
    {
        List<Data> data = new List<Data>();
        data.Add(new Data() { Id = 1, Name = "Dep 1", ParentId = 0, Level = 0 });
        data.Add(new Data() { Name = "Dep 2", ParentId = 1, Id = 2, Level = 1 });
        data.Add(new Data() { Name = "Dep 6", ParentId = 2, Id = 6, Level = 2 });
        data.Add(new Data() { Name = "Dep 4", ParentId = 1, Id = 4, Level = 1 });
        data.Add(new Data() { Name = "Dep 5", ParentId = 4, Id = 5, Level = 2 });
        data.Add(new Data() { Name = "Dep 3", ParentId = 0, Id = 3, Level = 0 });

        var result = data
                    .Where(c=>c.Level == 0)
                    .Select(c => new Result
                    {
                        Id = c.Id,
                        Name = c.Name,
                        Children = GetChildren(data, c.Id)
                    }).ToList();

        string resultJson = JsonConvert.SerializeObject(result);

        Console.ReadLine();
    }

    public static List<Result> GetChildren(List<Data> records, int parentId)
    {
        return records
                .Where(c => c.ParentId == parentId)
                .Select(c => new Result
                {
                    Id = c.Id,
                    Name = c.Name,
                    Children = GetChildren(records, c.Id)
                }).ToList();
    }
}

public class Data
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ParentId { get; set; }
    public int Level { get; set; }
}

public class Result
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Result> Children { get; set; }
}

<强>结果

[
  {
    "Id": 1,
    "Name": "Dep 1",
    "Children": [
      {
        "Id": 2,
        "Name": "Dep 2",
        "Children": [
          {
            "Id": 6,
            "Name": "Dep 6",
            "Children": []
          }
        ]
      },
      {
        "Id": 4,
        "Name": "Dep 4",
        "Children": [
          {
            "Id": 5,
            "Name": "Dep 5",
            "Children": []
          }
        ]
      }
    ]
  },
  {
    "Id": 3,
    "Name": "Dep 3",
    "Children": []
  }
]