Reputation: 13
We have the result set which is returned as a List with parent, child, and some more columns. We want to convert that into the Json with parent converting to Id and Child to Corrresponding nodes. see example below
Returned result:
parent | Child
ab | cd
ab | ef
ab | gh
cd | ij
cd | kl
ef | mn
ef | op
gh | qr
ij | st
and so on
expected Json:
{
"id": "ab",
"label": "ab",
"nodes": [
{
"id": "cd",
"label": "cd",
"nodes": [
{
"id": "ij",
"label": "ij",
"nodes": {
"id": "st",
"label": "st"
}
},
{
"id": "kl",
"label": "kl"
}
]
},
{
"id": "ef",
"label": "ef",
"nodes": [
{
"id": "mn",
"label": "mn"
},
{
"id": "op",
"label": "op"
}
]
},
{
"id": "gh",
"label": "gh",
"nodes": {
"id": "qr",
"label": "qr"
}
}
]
}
Hope Question is clear, Please let me know either C# or js utility that helps me in doing this.
Thanks in Advance
After some research,
public class TreeNode
{
public TreeNode()
{
nodes = List<TreeNode>();
}
String id {get; set;}
List<TreeNode> nodes {get; set;}
}
Method
public List<TreeNode> FlatToHierarchy(List<TreeNode> list)
{
var lookup = new Dictionary<string, TreeNode>();
// actual nested collection to return
List<TreeNode> nested = new List<TreeNode>();
foreach (TreeNode item in list)
{
if (lookup.ContainsKey(item.Parent))
{
lookup[item.Parent].Children.Add(item);
}
else
{
nested.Add(item);
}
lookup.Add(item.Part, item);
}
return nested;
}
But this returns,
ab
|
--cd
|
--ij
--cd
|
--kl
ab
|
--ef
|
--mn
--ef
|
--op
and so on, which is not expected data.
updated the question, so that it might be helpful for getting answers.
Thanks in Advance.
Upvotes: 0
Views: 121
Reputation: 13
Finally, we could write some code and it worked for us.
public List<TreeNode> FillRecursive(List<T> eqpList)
{
List<TreeNode> list = new List<TreeNode>();
//First Item in the list is our RootNode
var FirstEqp = eqpList.FirstOrDefault();
TreeNode RootNode = new TreeNode();
RootNode.text = FirstEqp.Parent;
RootNode.items.Add(new TreeNode() { text = FirstEqp.child });
foreach (EquipmentHierarchySPList eqp in eqpList)
{
GetTreeStructureFromList(eqp, list, RootNode);
}
return list;
}
public void GetTreeStructureFromList(T eqp, List<TreeNode> list,TreeNode RootNode)
{
bool found = false;
TreeNode FoundNode = GetTreeNode((list.Count != 0 ? RootNode : null), eqp.Parent, out found);
if (!FoundNode.IsNullValue())
FoundNode.items.Add(new TreeNode() { text = eqp.child });
else //this will execute only once.
list.Add(RootNode);
}
public TreeNode GetTreeNode(TreeNode RootNode, string findText,out bool found)
{
//if RootNode is Null , just return;
if (RootNode.IsNullValue())
{
found = false;
return null;
}
if (RootNode.text.Equals(findText))
{
found = true;
return RootNode;
}
for (int j = 0; ; j++)
{
if (j >= RootNode.items.Count)
{
found = false;
return RootNode;
}
var final = GetTreeNode(RootNode.items[j], findText,out found);
if (found == true)
return final;
if (final.IsNullValue())
return RootNode;
}
}
}
public class TreeNode
{
public TreeNode()
{
items = new List<TreeNode>();
}
public string text { get; set; }
public List<TreeNode> items { get; set; }
public bool TreeCompare(string findText)
{
return Convert.ToBoolean(text.CompareTo(findText));
}
}
Thanks For Suggestions/Help.
Upvotes: 0