tags:

views:

33

answers:

3

Hello All I have a Hashtable containing key and data in the following format.

Key            Data
--------------------
X1    ---- >   D1
X2    ---- >   D1
X3    ---- >   D1
X4    ---- >   D2
X5    ---- >   D2
X6    ---- >   D3

My requirement is to get a tree structure from the same data

i.e

D1
|------- X1
|--------X2
|--------X3

D2
|------- X4
|--------X5

D3
|------- X6

Can anyone help out with the logic ? Thanks in advance

A: 

I apologise for my question being too elusive. As you pointed out I'm trying to convert a map of key => data into a hierarchy of tree node objects.

rainmaker
outis
+2  A: 

You can do this

var data = new Dictionary<string, string>() 
{
    { "X1", "D1"},
    { "X2", "D1"},
    { "X3", "D2"},
};

var transform = data.GroupBy(m => m.Value)
                    .ToDictionary(m => m.Key, m => m.Select( g => g.Key )
                    .ToList()); 
tarn
A: 

You could do something this .

Hashtable  theHash;
theHash = new Hashtable();

theHash.Add("d1", new Hashtable());
theHash.Add("d2", new Hashtable());
theHash.Add("d3", new Hashtable());

Hashtable activeChild;
activeChild = (Hashtable)theHash["d1"];

activeChild.Add("x1", "myvalue");
activeChild.Add("x2", "myvalue2");
activeChild.Add("x3", "myvalue3");
Paul Farry