使用c#将平面数据结构转换为层次结构对象

时间:2011-06-15 04:32:52

标签: c#-4.0

我有以下平面数据结构。

ParentAttributeId AttributeId  List
----------------- -----------  ------
NULL              29          TestcaseCollection
29                30              EnclosureLeakageDielectricStrengthTest
30                31                  DeviceID
30                32                  ScannerOneLowChannel
30                33                  ScannerTwoLowChannel
29                34              EnclosureLeakageLeakageCurrentTest
34                35                  DeviceID
34                36                  ScannerOneLowChannel
34                37                  ScannerTwoLowChannel
29                38              PatientCircuitLeakageTest
38                39                  DeviceID
38                40                  ScannerOneLowChannel
38                41                  ScannerTwoLowChannel
29                42              SIPSOPDielectricStrengthTest
42                44                  ScannerOneHighChannel
42                45                  ScannerOneLowChannel
42                46                  ScannerTwoHighChannel
42                47                  ScannerTwoLowChannel
29                48              SIPSOPLeakageCurrentTest
48                49                  ScannerOneHighChannel
48                50                  ScannerOneLowChannel
48                51                  ScannerTwoHighChannel
48                52                  ScannerTwoLowChannel

我需要将以上平面数据结构转换为层次结构对象结构,如下所示。所以我的对象看起来像上面的“列表”列。我正在使用SQL Stored Proc来获取上述数据。我正在使用C#。

对象层次结构

29
  |
  30 
   |  31
   |  32
   |  33
   |   
  34
   |  35
   |  36
    |37
  38

非常感谢任何帮助。

此致 二重

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

1)创建一个节点类

class Node
{
    public int ParentId { get; private set; }
    public int Id { get; private set; }
    public string Label { get; private set; }
    public Node Parent { get; set; }
    public List<Node> Children { get; } = new List<Node>();

    public Node(int parentId, int id, string lable)
    {
        ParentId = parentId;
        Id = id;
        Label = lable;
    }

    public void AddChild(Node child)
    {
        child.Parent = this;
        Children.Add(child);
    }

    public void Trace(int indent = 1)
    {
        Enumerable.Range(0, indent).ToList().ForEach(i => Console.Write(" - "));
        Console.WriteLine(Label);
        Children.ForEach(c => c.Trace(indent + 1));
    }
}

2)根据您的平面数据创建节点对象,并将其添加到字典中

var data = new List<DataRow>() {
            new DataRow { ParentId = 0, Id = 1, Label = "parent" },
            new DataRow { ParentId = 1, Id = 2, Label = "child 1" },
            new DataRow { ParentId = 1, Id = 3, Label = "child 2" },
            new DataRow { ParentId = 2, Id = 4, Label = "grand child 1" },
            new DataRow { ParentId = 2, Id = 5, Label = "grand child 2" }
        };

Dictionary<int, Node> nodes = data.ToDictionary(d => d.Id, d => new Node(d.ParentId, d.Id, d.Label));

3)通过遍历在父节点上调用AddChild的所有节点来构建层次结构

foreach (var node in nodes.Skip(1))
    nodes[node.Value.ParentId].AddChild(node.Value);

如果在顶部节点上调用Trace(),输出将如下所示:

 - parent
 -  - child 1
 -  -  - grand child 1
 -  -  - grand child 2
 -  - child 2

答案 1 :(得分:0)

你看过AutoMapper吗?

不确定这是否是您所需要的,但我经常将其从一种格式转换为对象模型。

另一种选择可能是使用LINQ查询您拥有的数据并为您创建模型。

我认为你可以这样说,而且这是未经测试的;

从dataList中选择select new {....

其中new将是您正在创建的新对象。

但是,我认为也许在列表中迭代的蛮力方法可能仍然是这样。

修改

this might help