Last active
August 11, 2016 09:36
-
-
Save neo125874/0cbe1c168320765a0663abece728851b to your computer and use it in GitHub Desktop.
Flat Hierarchy Conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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