//Nested tree list, result list contains list of lists
List<NavigationItem> allitems= result;
//Flatten the list
List<NavigationItem> childitems = Flatten(allitems, a=>a.Items).ToList();
public List<NavigationItem> Flatten(List<NavigationItem> rootLevel, Func<NavigationItem, List<NavigationItem>> nextLevel)
{
List<NavigationItem> accumulation = new List<NavigationItem>();
int i = 0;
flattenLevel(accumulation, rootLevel, nextLevel, i);
return accumulation;
}
private void flattenLevel(List<NavigationItem> accumulation, List<NavigationItem> currentLevel, Func<NavigationItem, List<NavigationItem>> nextLevel, int level)
{
level++;
foreach (NavigationItem item in currentLevel)
{
item.LevelNo = level;
item.Level = "Level " + level.ToString();
accumulation.Add(item);
flattenLevel(accumulation, nextLevel(item), nextLevel, level);
}
}
List<NavigationItem> allitems= result;
//Flatten the list
List<NavigationItem> childitems = Flatten(allitems, a=>a.Items).ToList();
public List<NavigationItem> Flatten(List<NavigationItem> rootLevel, Func<NavigationItem, List<NavigationItem>> nextLevel)
{
List<NavigationItem> accumulation = new List<NavigationItem>();
int i = 0;
flattenLevel(accumulation, rootLevel, nextLevel, i);
return accumulation;
}
private void flattenLevel(List<NavigationItem> accumulation, List<NavigationItem> currentLevel, Func<NavigationItem, List<NavigationItem>> nextLevel, int level)
{
level++;
foreach (NavigationItem item in currentLevel)
{
item.LevelNo = level;
item.Level = "Level " + level.ToString();
accumulation.Add(item);
flattenLevel(accumulation, nextLevel(item), nextLevel, level);
}
}
No comments:
Post a Comment