列表生成层次结构编号

时间:2015-03-24 13:17:31

标签: c# .net entity-framework hierarchy

我想制作一个通过ParentId生成层次结构的方法

这是我的Db课程:

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

和我的模型类

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

我没有在db中存储Hierarchy列。 我想通过ParentId设置Hierarchy属性,如

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引用自身(卡丁车)

1 个答案:

答案 0 :(得分:2)

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

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, "")

希望这有帮助。