niju
niju

Reputation: 115

Converting flat data structure into hierarchy Object using c#

I have the following Flat Data Structure.

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

I need to convert above flat data structure into a hierarchy Object structure like below. So my Object looks like the "List" Column above. I am using SQL Stored Proc to get the above data. I am using C#.

Object hierarchy

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

Any help would be greatly appreciated.

Regards Niju

Upvotes: 3

Views: 2106

Answers (2)

user1797859
user1797859

Reputation: 11

You could try something like this:

1) Create a node class

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) Create node objects from you flat data and add them to a dictionary

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) Build the hierarchy by looping through all nodes calling AddChild on the parent

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

If you call Trace() on the top node, the output will look like this:

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

Upvotes: 1

griegs
griegs

Reputation: 22760

Have you looked at AutoMapper?

Not sure this is what you need but it's what I use to often convert from one format to an object model.

Another alternative might be to use LINQ to query the data you have and to create the model for you.

I think you can say something like, and this is untested;

select from dataList select new {....

where new would be the new object you are creating.

However, i think maybe the brute force approach of iterating through your list might still be the way.

edit

this might help.

Upvotes: 0

Related Questions