Skip to content

Instantly share code, notes, and snippets.

@neo125874
Last active August 11, 2016 09:36
Show Gist options
  • Save neo125874/0cbe1c168320765a0663abece728851b to your computer and use it in GitHub Desktop.
Save neo125874/0cbe1c168320765a0663abece728851b to your computer and use it in GitHub Desktop.
Flat Hierarchy Conversion
private static List<Category> FlatToHierarchy(List<Category> list, int parentID = 0)
{
return (from l in list
where l.ctgParentID == parentID
select new Category
{
ctgID = l.ctgID,
ctgParentID = l.ctgParentID,
ctgName = l.ctgName,
childCategories = FlatToHierarchy(list, l.ctgID),
ctgExist = l.ctgExist
}).ToList();
}
private static IEnumerable<Category> HierarchyToFlat(Category parent)
{
yield return parent;
foreach (var child in parent.childCategories)
foreach (var relative in HierarchyToFlat(child))
yield return relative;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment