private static void AddChildren(DocumentCategoryList hierarchy, DocumentCategory parentDocumentCategory, int depth, DocumentCategoryList allDocumentCategories) { foreach (var checkDocumentCategory in allDocumentCategories) { if (checkDocumentCategory.ParentDocumentCategoryID == parentDocumentCategory.ID) { var childDocumentCategory = checkDocumentCategory; childDocumentCategory["Depth"].ValueObject = depth; hierarchy.Add(childDocumentCategory); AddChildren(hierarchy, childDocumentCategory, depth + 1, allDocumentCategories); } } }
/// <summary> /// Returns a list of all page, in order with children underneath their parents and with an extra virtual field called Depth /// </summary> /// <returns></returns> public static DocumentCategoryList GetDocumentCategoryHierarchy() { var documentCategoryHierarchy = new DocumentCategoryList(); var documentCategories = DocumentCategoryList.Load(new Sql("select *, 0 as Depth from DocumentCategory order by SortPosition,DocumentCategoryID")); foreach (var documentCategory in documentCategories) { //page["Depth"].ValueObject = GetDepth(page, pages); if (documentCategory.ParentDocumentCategory == null) { // top level page documentCategory["Depth"].ValueObject = 0; documentCategoryHierarchy.Add(documentCategory); AddChildren(documentCategoryHierarchy, documentCategory, 1, documentCategories); } } return(documentCategoryHierarchy); }