| 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リストに出力しようとしています。ここでスタックオーバーフローでさまざまな回答を試みて、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;
}