我有两种方法可以从平面列表返回动态层次结构。第一个在这里使用递归方法效果很好:(ID/ParentID) list to Hierarchical list。
我现在正在尝试做同样的事情,只是这次只显示那些已保存报告输出的类别和报告。我不知道从哪里开始,因为我发现的一切都是从根向下构建的,我需要从下往上。
我现在在我的第一种方法中得到了类似的东西:
Category 1
|_Sub Category 1
|_Report 1
|_Report 2
|_Saved Output
Category 2
|_Sub Category 2
| |_Report 3
| |_Report 4
|_Sub Category 3
|_Report 5
|_Report 6
|_Saved Output
Category 3
|_Sub Category 4
|_Report 7
我在第二种方法中想要的是:
Category 1
|_Sub Category 1
|_Report 2
|_Saved Output
Category 2
|_Sub Category 3
|_Report 6
|_Saved Output
这是我的基本测试结构:
class Flat
{
public int id { get; set; }
public int parentId { get; set; }
public string name { get; set; }
public bool isOutput { get; set; }
public Flat(int i, int pid, string n, bool o)
{
this.id = i;
this.parentId = pid;
this.name = n;
this.isOutput = o;
}
}
class MyClass
{
public int id { get; set; }
public int parentId { get; set; }
public string name { get; set; }
public bool isOutput { get; set; }
public List<MyClass> children { get; set; }
public MyClass()
{
this.children = new List<MyClass>();
}
}
List<Flat> items = new List<Flat>()
{
new Flat(1,0,"Category 1",false),
new Flat(4,1,"Sub Category 1",false),
new Flat(8,4,"Report 1",false),
new Flat(9,4,"Report 2",false),
new Flat(15,9,"Saved Output",true),
new Flat(2,0,"Category 2",false),
new Flat(5,2,"Sub Category 2",false),
new Flat(10,5,"Report 3",false),
new Flat(11,5,"Report 4",false),
new Flat(6,2,"Sub Category 3",false),
new Flat(12,6,"Report 5",false),
new Flat(13,6,"Report 6",false),
new Flat(16,13,"Saved Output",true),
new Flat(3,0,"Category 3",false),
new Flat(7,3,"Sub Category 4",false),
new Flat(14,7,"Report 7",false)
};