fork(7) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var samples = new[] {
  10. new Category { Id = 1, ParentCategoryId = 8, SortOrder = 1, Text = "Firefall" },
  11. new Category { Id = 2, ParentCategoryId = 8, SortOrder = 2, Text = "Left 4 Dead 2" },
  12. new Category { Id = 3, ParentCategoryId = 8, SortOrder = 3, Text = "Renegade X" },
  13. new Category { Id = 4, ParentCategoryId = 2, SortOrder = 2, Text = "Survival" },
  14. new Category { Id = 5, ParentCategoryId = 0, SortOrder = 1, Text = "General" },
  15. new Category { Id = 6, ParentCategoryId = 8, SortOrder = 4, Text = "Battlefield 4" },
  16. new Category { Id = 7, ParentCategoryId = 2, SortOrder = 1, Text = "Versus" },
  17. new Category { Id = 8, ParentCategoryId = 0, SortOrder = 2, Text = "Games" },
  18. new Category { Id = 9, ParentCategoryId = 0, SortOrder = 3, Text = "Army Restricted Area" },
  19. new Category { Id = 10, ParentCategoryId = 9, SortOrder = 1, Text = "Kool Kids Klub" }
  20. };
  21.  
  22. var categories = new List<Category>(samples);
  23.  
  24. // hierarchycal
  25. var categoryTree = CategoryTree.Create(categories, o => o.ParentCategoryId == 0);
  26. // recursively called Console.WriteLine on every node
  27. PrintNodes(categoryTree);
  28.  
  29. Console.WriteLine(new string('-',80));
  30.  
  31. var flatTree = categoryTree.Flatten();
  32. foreach(var category in flatTree) {
  33. Console.WriteLine(category.DisplayText);
  34. }
  35. }
  36.  
  37. public static void PrintNodes(CategoryTree tree) {
  38. if (tree == null || !tree.Any()) return;
  39.  
  40. foreach(var node in tree) {
  41. Console.WriteLine(node.DisplayText);
  42. PrintNodes(node.Children);
  43. }
  44. }
  45. }
  46.  
  47. public class Category {
  48. public int Id { get; set; }
  49. public int ParentCategoryId { get; set; }
  50. public int SortOrder { get; set; }
  51. public string Text { get; set; }
  52. }
  53.  
  54. public class CategoryNode : Category {
  55. public CategoryNode(Category category) {
  56. Id = category.Id;
  57. ParentCategoryId = category.ParentCategoryId;
  58. SortOrder = category.SortOrder;
  59. Text = category.Text;
  60. }
  61.  
  62. public CategoryTree Children { get; set; }
  63. public int Level { get; set;}
  64. public string DisplayText {
  65. get {
  66. return string.Concat(new string('.', Level*2), Text);
  67. }
  68. }
  69. }
  70.  
  71. public class CategoryTree : IEnumerable<CategoryNode> {
  72. private List<CategoryNode> innerList = new List<CategoryNode>();
  73.  
  74. public CategoryTree(IEnumerable<CategoryNode> nodes) {
  75. innerList = new List<CategoryNode>(nodes);
  76. }
  77.  
  78. public IEnumerator<CategoryNode> GetEnumerator()
  79. {
  80. return innerList.GetEnumerator();
  81. }
  82.  
  83. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  84. {
  85. return this.GetEnumerator();
  86. }
  87.  
  88. public IEnumerable<CategoryNode> Flatten() {
  89. foreach(var category in innerList.OrderBy(o => o.SortOrder)) {
  90. yield return category;
  91. if (category.Children != null) {
  92. foreach(var child in category.Children.Flatten()) {
  93. yield return child;
  94. }
  95. }
  96. }
  97. }
  98.  
  99. public static CategoryTree Create(IEnumerable<Category> categories, Func<Category, bool> parentPredicate, int level = 0) {
  100. var nodes = categories
  101. .Where(parentPredicate)
  102. .OrderBy(o => o.SortOrder)
  103. .Select(item =>
  104. new CategoryNode(item) {
  105. Level = level,
  106. Children = Create(categories, o => o.ParentCategoryId == item.Id, level + 1)
  107. });
  108.  
  109. return new CategoryTree(nodes);
  110. }
  111. }
Success #stdin #stdout 0.05s 35016KB
stdin
Standard input is empty
stdout
General
Games
..Firefall
..Left 4 Dead 2
....Versus
....Survival
..Renegade X
..Battlefield 4
Army Restricted Area
..Kool Kids Klub
--------------------------------------------------------------------------------
General
Games
..Firefall
..Left 4 Dead 2
....Versus
....Survival
..Renegade X
..Battlefield 4
Army Restricted Area
..Kool Kids Klub