| Level1 | Level2 | Level3 | Level4|
| ELECTRONICS | TELEVISIONS | NULL | NULL |
| ELECTRONICS | TELEVISIONS | LCD | NULL |
| ELECTRONICS | PC | NULL | NULL |
| ELECTRONICS | PORTABLE ELECTRONICS | MP3 PLAYERS | FLASH |
| ELECTRONICS | PORTABLE ELECTRONICS | CD PLAYERS | NULL |
| ELECTRONICS | PORTABLE ELECTRONICS | 2 WAY RADIOS | NULL |
在一个遗留数据库中,我有这个表,我试图在一个很好的层次结构 html ul li 列表中打印出来。我一直在 stackoverflow 上尝试不同的答案以在 C# 中构建对象,但它们都有 parentID 和 childID,并且在这种情况下对我有用。
我正在尝试这段代码,但我知道它真的很糟糕,
List<Node> FlatToHierarchy(IList<SearchWord> list)
{
List<Node> nodes = new List<Node>();
foreach (SearchWord x in list)
{
if (x.Level2 != " " && x.Level3 == " ")
{
Node node = new Node();
node.Parent = x.Level1;
node.Child = x.Level2;
node.Keyword = x.Keyword;
nodes.Add(node);
//dict.Add(node.Parent, node);
}
if (x.Level3 != " " && x.Level4 == " ")
{
Node node = new Node();
node.Parent = x.Level2;
node.Child = x.Level3;
node.Keyword = x.Keyword;
nodes.Add(node);
//dict.Add(node.Parent, node);
}
if (x.Level4 != " ")
{
Node node = new Node();
node.Parent = x.Level3;
node.Child = x.Level4;
node.Keyword = x.Keyword;
nodes.Add(node);
//dict.Add(node.Parent, node);
}
}
return nodes;
}