平面数据的层次结构w / out parentID childID

时间:2012-11-07 14:21:46

标签: c# hierarchy

| 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;
    }

2 个答案:

答案 0 :(得分:1)

为了构建真正的层次结构,您的class Node将需要在类中包含子项。将List<Node>添加到Node以及Add方法,因此数据包含层次结构。然后你可以建立一个html li / ul列表。例如,Node类可能如下所示:

public class Node
{
    public string Name { get; set; } // Current node name
    public string Parent { get; set; }
    public string Keyword { get; set; }
    public int Level { get; set; } // Optional

    private List<Node> _children = new List<Node>();
    public List<Node> Children { get { return _children; } }

    public Node AddChild(Node child)
    {
        _children.Add(child);
        return this;
    }   
}

或者您可以Parent对节点进行引用:

public class Node
{
    public Node Parent { get; set; }
    public string ParentName { get { Parent != null ? Parent.Name : null; } }
    // ...
}

然后构建html li / ul列表,递归遍历Node结构。

编辑:实际上,现在我想起来了,你可能只想从数据中构建一个分层的XML文档。然后你可以从xml:

构建ul / li
public class Data
{
    public string Level1 { get; set; }
    public string Level2 { get; set; }
    public string Level3 { get; set; }
    public string Level4 { get; set; }
}

public Data[] sampleData = new Data[] {
    new Data { Level1 = "ELECTRONICS", Level2 = "TELEVISIONS", Level3 = null, Level4 = null },
    new Data { Level1 = "ELECTRONICS", Level2 = "TELEVISIONS", Level3 = "LCD", Level4 = null },
    new Data { Level1 = "ELECTRONICS", Level2 = "PC", Level3 = null, Level4 = null },
    new Data { Level1 = "ELECTRONICS", Level2 = "PORTABLE ELECTRONICS", Level3 = "MP3 PLAYERS", Level4 = "FLASH" },
    new Data { Level1 = "ELECTRONICS", Level2 = "PORTABLE ELECTRONICS", Level3 = "CD PLAYERS", Level4 = null },
    new Data { Level1 = "ELECTRONICS", Level2 = "PORTABLE ELECTRONICS", Level3 = "2 WAY RADIOS", Level4 = null },
};

XElement AddNode(string name)
{
    return new XElement("Node", 
        new XAttribute("Name", name));
}

// NOTE: This is not optimized, but you get the idea...
XDocument BuildHierarchy(IEnumerable<Data> data)
{
    XElement root = new XElement("Root");
    XDocument xdoc = new XDocument(root);
    XElement level1 = null;
    XElement level2 = null;
    XElement level3 = null;
    XElement level4 = null;

    foreach (var item in data)
    {
        // Assumes item.Level1 is never empty...
        if (level1 == null || string.Compare(item.Level1, level1.Attribute("Name").Value) != 0)
        {
            level1 = AddNode(item.Level1);
            root.Add(level1);
            level2 = null;
            level3 = null;
            level4 = null;
        }

        if (string.IsNullOrWhiteSpace(item.Level2))
        {
            level2 = null;
            level3 = null;
            level4 = null;
            continue;
        }

        if (level2 == null || string.Compare(item.Level2, level2.Attribute("Name").Value) != 0)
        {
            level2 = AddNode(item.Level2);
            level1.Add(level2);
            level3 = null;
            level4 = null;
        }

        if (string.IsNullOrWhiteSpace(item.Level3))
        {
            level2 = null;
            level3 = null;
            level4 = null;
            continue;
        }

        if (level3 == null || string.Compare(item.Level3, level3.Attribute("Name").Value) != 0)
        {
            level3 = AddNode(item.Level3);
            level2.Add(level3);
            level4 = null;
        }

        if (string.IsNullOrWhiteSpace(item.Level4))
        {
            level4 = null;
            continue;
        }

        if (level4 == null || string.Compare(item.Level4, level4.Attribute("Name").Value) != 0)
        {
            level4 = AddNode(item.Level4);
            level3.Add(level4);
        }
    }

    return xdoc;
}

答案 1 :(得分:0)

我会把这个表放到一个行集合中,并将每行的行分组,但这取决于你想要最终得到的数据结构。

如果您需要的只是HTML中的分层列表且只有4列,请相应地按Level1Level2Level3Level4排序数据库查询,然后循环通过它们。保留一个指示器以查看级别是否已更改并根据需要输出HTML代码。