0

I have following method which return list of an item in C#.

public List<LevelItem> GetItemList()
    {
        List<LevelItem> items = new List<LevelItem>
        {
            new LevelItem(){ ItemID = 70, Name="Home", ChildCount = 1, Level = 0, ParentID = 0 },
            new LevelItem(){ ItemID = 71, Name="Pages", ChildCount = 1, Level = 0, ParentID = 0 },
            new LevelItem(){ ItemID = 72, Name="Pages II", ChildCount = 1, Level = 0, ParentID = 0 },
            new LevelItem(){ ItemID = 77, Name="My World", ChildCount = 1, Level = -1, ParentID = 0 },
            new LevelItem(){ ItemID = 79, Name="Level 3", ChildCount = 0, Level = 0, ParentID =  0},
            new LevelItem(){ ItemID = 73, Name="Page III", ChildCount = 0, Level = 0, ParentID = 71},
            new LevelItem(){ ItemID = 74, Name="Page IV", ChildCount = 0, Level = 0, ParentID = 70 },
            new LevelItem(){ ItemID = 75, Name="Level 1", ChildCount = 1, Level = 0, ParentID = 72 },
            new LevelItem(){ ItemID = 76, Name="Hello 1", ChildCount = 1, Level = 0, ParentID = 77 },
            new LevelItem(){ ItemID = 78, Name="Level 2", ChildCount = 0, Level = -1, ParentID = 76 }
        };
        return items;
    }

And a class with following properties.

public class LevelItem
    {
        public int ItemID { get; set; }
        public string Name { get; set; }
        public int ChildCount { get; set; }
        public int ParentID { get; set; }
        public int Level { get; set; }
        public  List<LevelItem> ItemList;
    }

Basically I want to convert the above list into nested list based on their ParentID and ItemID properties. I want to get result something like:

{ 
    ItemID = 70, 
    Name="Home", 
    ChildCount = 1, 
    Level = 0, 
    ParentID = 0,
    { 
        ItemID = 74, 
        Name="Page IV", 
        ChildCount = 0, 
        Level = 0, 
        ParentID = 70 
    }     
}
{ 
    ItemID = 71, 
    Name="Pages", 
    ChildCount = 1, 
    Level = 0, 
    ParentID = 0, 
    { 
        ItemID = 73, 
        Name="Page III", 
        ChildCount = 0, 
        Level = 0, 
        ParentID = 71
    } 
}
{ 
    ItemID = 72, 
    Name="Pages II", 
    ChildCount = 1, 
    Level = 0, 
    ParentID = 0 
    { 
        ItemID = 75, 
        Name="Level 1", 
        ChildCount = 1, 
        Level = 0, 
        ParentID = 72 
    }
}
{ 
    ItemID = 77, 
    Name="My World", 
    ChildCount = 1, 
    Level = -1, 
    ParentID = 0,
    {   
        ItemID = 76, 
        Name="Hello 1", 
        ChildCount = 1, 
        Level = 0, 
        ParentID = 77 , 
        { 
            ItemID = 78, 
            Name="Level 2", 
            ChildCount = 0, 
            Level = -1, 
            ParentID = 76 
        }
    }
}
{ 
    ItemID = 79, 
    Name="Level 3", 
    ChildCount = 0, 
    Level = 0, 
    ParentID =  0
}

Is it possible?

Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
  • It's definitely possible, what have you tried? Just create a list of root items where ParentId equals 0, then for each item in that list, recursively attach their children? – CodeCaster Sep 27 '18 at 10:02
  • Sure, here is the code https://dotnetfiddle.net/HOrH1S – fubo Sep 27 '18 at 10:09
  • @fubo thank you for your answer but that is not what I am looking for. – Albert Einstein Sep 27 '18 at 10:12
  • Both duplicates use recursion. Both can run faster if the items can be indexed by ParentID so that searching for one item's children doesn't require traversing the entire list. LINQ's `ToLookup()` can create a "multivalue" dictionary, eg : `items.ToLookup(it=>it.ParentID,it=>it)`. Instead of `list.Where(x => x.ParentID == parent)` one could write `lookup[parent]` – Panagiotis Kanavos Sep 27 '18 at 10:19
  • @PanagiotisKanavos thank you very much. That duplicates solved my problem. – Albert Einstein Sep 27 '18 at 10:45

0 Answers0