1

Usually we convert flat list to hierarchical list but in my case I want to convert hierarchical list to flat list.

I have a hierarchical list<T> and I want convert this hierarchical list into flat list<T> class.

Let's say I have a below hierarchical list

 Parent1
  --- Child1 ,F1
  ----Child2, F2
  ----Child2, F3
 Parent2
  ----Child2,F2
  ----Child4,F6
  Parent3
  --- Child1 ,F1

I need output like below:

 Parent1, Child1,F1
 Parent1, Child2,F2
 Parent1, Child2,F3
 Parent2,Child2,F2
 Parent2,Child4, F6
 Parent3, Child1,F1
halfer
  • 19,824
  • 17
  • 99
  • 186
user1818042
  • 75
  • 1
  • 11
  • We'd need to see the actual class structure to give a meaningful answer, but I would try using `SelectMany` and come back when you're stuck. – D Stanley Apr 25 '16 at 17:19
  • 1
    Possible duplicate of [How to flatten tree via LINQ?](http://stackoverflow.com/questions/11830174/how-to-flatten-tree-via-linq) – Ivan Stoev Apr 25 '16 at 17:36
  • Hi D Stanley, i have just given above as example for your understanding , i have very bis class structure which i can't provide beacz class is very huge. as you mentioned using selectmany how can i do it..could you please some code with example. – user1818042 Apr 26 '16 at 05:16

2 Answers2

0

It may not be the most optimized solution (it could be further minimized I think, and I have a more concise version - but this form is something I'm using the most)

It's what I've been using

public static IEnumerable<T> FlattenHierarchy<T>(this T node,
                                Func<T, IEnumerable<T>> getChildEnumerator)
{
    yield return node;
    if (getChildEnumerator(node) != null)
    {
        foreach (var child in getChildEnumerator(node))
        {
            foreach (var childOrDescendant
                        in child.FlattenHierarchy(getChildEnumerator))
            {
                yield return childOrDescendant;
            }
        }
    }
}

and you can use it like

folder.FlattenHierarchy(x => x.SubFolders)
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
0

Finally i got the solution:

public class RNode 
        {
            public string Id;
            public long ID;
            public string name;
            public string subTitle;
            public IList<RNode> children;          
        } 
          public class FlatObj //For Data format
        {
            public long Id;
            public long ParentId;
            public long Position;
            public string name;
            public string subTitle;
        }
        List<FlatObj> GlobalFlatObj = new List<FlatObj>();
        List<long> ParentIdList = new List<long>();
        long CurrentParentId=0;
        long CurrentPosition = 0;

    public List<FlatObj> FlatData(IList<RNode> HData) //Converting Heirarchical to Flat
    {
        foreach (RNode node in HData)
        {
            FlatObj ObjFlatObj = new FlatObj();
            ObjFlatObj.Id = node.ID;
            ObjFlatObj.name = node.name;
            ObjFlatObj.ParentId = CurrentParentId;
            ObjFlatObj.Position = CurrentPosition;
            GlobalFlatObj.Add(ObjFlatObj);
            if (node.children.Count > 0)
            {
                CurrentParentId = node.ID;
                ParentIdList.Add(node.ID);
                FlatData(node.children);
            }
            CurrentPosition++;
        }
        if (ParentIdList.Count > 0)
        {
            ParentIdList.RemoveAt(ParentIdList.Count - 1);
            if (ParentIdList.Count > 0)
                CurrentParentId = ParentIdList[ParentIdList.Count - 1];
            CurrentPosition = 0;
        }
        return GlobalFlatObj;
    }
    public dynamic Test(List<RNode> EmployeeHierarchy)
    {
        var HierarchyResult = FlatData(EmployeeHierarchy); //Calling
        return Ok(HierarchyResult);
    }

Input:

[
    {
    "$id": "1",
    "ID": 1,
    "name": "root",
    "subTitle": "root",
    "children": [
        {
            "$id": "2",
            "ID": 2,
            "name": "child 1",
            "subTitle": "child 1",
            "children": [
                {
                    "$id": "3",
                    "ID": 5,
                    "name": "grandchild",
                    "subTitle": "grandchild",
                    "children": []
                }]
        },                  
        {
            "$id": "4",
            "ID": 3,
            "name": "child 2",
            "subTitle": "child 2",
            "children": []
        },
        {
            "$id": "5",
            "ID": 4,
            "name": "child 3",
            "subTitle": "child 3",
            "children": []
        }       
    ]
    }
    ]

Output:

[
{
    "$id": "1",
    "Id": 1,
    "ParentId": 0,
    "Position": 0,
    "name": "root",
    "subTitle": null
},
{
    "$id": "2",
    "Id": 2,
    "ParentId": 1,
    "Position": 0,
    "name": "child 1",
    "subTitle": null
},
{
    "$id": "3",
    "Id": 5,
    "ParentId": 2,
    "Position": 0,
    "name": "grandchild",
    "subTitle": null
},
{
    "$id": "4",
    "Id": 3,
    "ParentId": 1,
    "Position": 1,
    "name": "child 2",
    "subTitle": null
},
{
    "$id": "5",
    "Id": 4,
    "ParentId": 1,
    "Position": 2,
    "name": "child 3",
    "subTitle": null
}

]