列表生成层次编号

List Generate hierarchy number

我想创建一个方法来通过 ParentId 生成层次结构

这是我的数据库 class :

  public class Kart
    {
        public long Id { get; set; }
        public long? ParentId { get; set; }
        public string Name { get; set; }       
    }

和我的模特Class

    public class KartModel
        {
            public long Id { get; set; }            
            public long? ParentId { get; set; }
            public string Hierarchy { get; set; }
            public string Name { get; set; }       
        }

我不在数据库中存储层次结构列。 我想通过 ParentId 设置层次结构 属性,例如

1
 1.1
   1.1.1
   1.1.2
   1.1.3
 1.2
 1.3
1 //(if ParentId == null start 1)
 1.1

谢谢。

编辑:ParentId 引用自身 (Kart)

递归方法是建立层次结构的最简单方法。下面的代码是一个控制台应用程序,它生成层次结构以便您询问。

class Program
{
    public class Kart
    {
        public long Id { get; set; }
        public long? ParentId { get; set; }
        public string Name { get; set; }
    }
    public class KartVM
    {
        public long Id { get; set; }
        public long? ParentId { get; set; }
        public string Hierarchy { get; set; }
        public string Name { get; set; }
    }

    static void Main(string[] args)
    {
        List<Kart> list = new List<Kart>
        {
            new Kart {Id = 1, ParentId = null, Name = "Main Content1"},
            new Kart {Id = 2, ParentId = 1, Name = "Main Content1"},
            new Kart {Id = 3, ParentId = 1, Name = "Main Content1"},
            new Kart {Id = 4, ParentId = 1, Name = "Main Content1"},
            new Kart {Id = 5, ParentId = 3, Name = "Main Content1"},
            new Kart {Id = 6, ParentId = 3, Name = "Main Content1"},
            new Kart {Id = 7, ParentId = 4, Name = "Main Content1"},
            new Kart {Id = 8, ParentId = 4, Name = "Main Content1"},
            new Kart {Id = 9, ParentId = 8, Name = "Main Content1"},
            new Kart {Id = 10, ParentId = 8, Name = "Main Content1"},
            new Kart {Id = 11, ParentId = 8, Name = "Main Content1"},
            new Kart {Id = 12, ParentId = 11, Name = "Main Content1"},
            new Kart {Id = 13, ParentId = 11, Name = "Main Content1"},
            new Kart {Id = 14, ParentId = 13, Name = "Main Content1"},
            new Kart {Id = 15, ParentId = null, Name = "Main Content1"},
            new Kart {Id = 16, ParentId = 15, Name = "Main Content1"},
            new Kart {Id = 17, ParentId = 16, Name = "Main Content1"},
            new Kart {Id = 18, ParentId = 17, Name = "Main Content1"},
            new Kart {Id = 19, ParentId = 18, Name = "Main Content1"},




        };
        List<KartVM> theResult = new List<KartVM>();
        GetHierachicalList(list, theResult, null, "");

        foreach(KartVM t in theResult)
        {
            Console.WriteLine(t.Hierarchy);
        }
        Console.ReadLine();
    }
    static void GetHierachicalList(List<Kart> kart, List<KartVM> kartVM, Kart currentNode, string curH)
    {
        List<Kart> tmp = new List<Kart>();
        if (currentNode == null)
            tmp = kart.Where(c => c.ParentId == null).ToList();
        else
            tmp = kart.Where(c => c.ParentId == currentNode.Id).ToList();
        int count = 1;
        foreach(Kart k in tmp)
        {

            KartVM tmpVM = new KartVM { Id = k.Id, Name = k.Name, ParentId = k.ParentId };
            tmpVM.Hierarchy += curH + "." + count.ToString();
            if (tmpVM.Hierarchy.StartsWith("."))
                tmpVM.Hierarchy = tmpVM.Hierarchy.Remove(0, 1);
            kartVM.Add(tmpVM);
            count++;
            GetHierachicalList(kart, kartVM, k, tmpVM.Hierarchy);

        }
    }
}

在这种情况下,方法是递归的 GetHierachicalList

对运行的调用如下:

GetHierachicalList(list, theResult, null, "")

希望对您有所帮助。