Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5.  
  6. namespace Console472
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. var treeData = new GroupedTreeBranches()
  13. {
  14. DiscountGroup = new List<DiscountGroup>()
  15. {
  16. new DiscountGroup
  17. {
  18. Name = "DG1",
  19. ID = 1
  20. },
  21. new DiscountGroup
  22. {
  23. Name = "DG2",
  24. ID = 2
  25. },
  26. new DiscountGroup
  27. {
  28. Name = "DG3",
  29. ID = 3
  30. }
  31. },
  32. SubGroup = new List<SubGroup>()
  33. {
  34. new SubGroup
  35. {
  36. Name = "SG 1",
  37. ID = 200,
  38. Parent = new DiscountGroup(){ID = 1, Name = "DG1"}
  39. },
  40. new SubGroup
  41. {
  42. Name = "SG 2",
  43. ID = 201,
  44. Parent = new DiscountGroup(){ID = 1, Name = "DG1"}
  45. },
  46. new SubGroup
  47. {
  48. Name = "SG 3",
  49. ID = 202,
  50. Parent = new DiscountGroup(){ID = 2, Name = "DG2"}
  51. },
  52. new SubGroup
  53. {
  54. Name = "SG 4",
  55. ID = 203,
  56. Parent = new DiscountGroup(){ID = 2, Name = "DG2"}
  57. }
  58. },
  59. PriceIntervals = new List<PriceIntervals>()
  60. {
  61. new PriceIntervals
  62. {
  63. Name = "Price 1",
  64. ID = 100,
  65. Parent = new DiscountGroup { ID = 1, Name = "DG1"}
  66. },
  67. new PriceIntervals
  68. {
  69. Name = "Price 2",
  70. Parent = new DiscountGroup { ID = 1, Name = "DG1"}
  71. },
  72. new PriceIntervals
  73. {
  74. Name = "Price 2",
  75. ID = 101,
  76. Parent = new DiscountGroup { ID = 2, Name = "DG2"}
  77. },
  78. },
  79. Rotation = new List<Rotation>()
  80. {
  81. new Rotation
  82. {
  83. Name = "Rot 1",
  84. ID = 20,
  85. Parent = new DiscountGroup(){ID = 200, Name = "SG 1"},
  86. },
  87. new Rotation
  88. {
  89. Name = "Rot 2",
  90. ID = 21,
  91. Parent = new SubGroup(){ID = 200, Name = "SG 1"},
  92. },
  93. new Rotation
  94. {
  95. Name = "Rot 3",
  96. ID = 22,
  97. Parent = new SubGroup(){ID = 201, Name = "SG 2"},
  98. },
  99. new Rotation
  100. {
  101. Name = "Rot 4",
  102. ID = 23,
  103. Parent = new SubGroup(){ID = 201, Name = "SG 2"},
  104. },
  105. }
  106. };
  107.  
  108. var tn = new TreeNode();
  109.  
  110. foreach (var item in treeData.DiscountGroup)
  111. {
  112. tn.AddChild(item);
  113.  
  114. var sg = treeData.SubGroup.Where(w => w.Parent.Name == item.Name);
  115. Each(sg, a => item.AddChild(a));
  116.  
  117. var pi = treeData.PriceIntervals.Where(w => w.Parent.Name == item.Name);
  118. Each(pi, a => item.AddChild(a));
  119.  
  120. foreach (var item2 in item.Children.Where(w => w.GetType() == typeof(SubGroup)))
  121. {
  122. var rot = treeData.Rotation.Where(w => w.Parent.GetType() == typeof(SubGroup) && w.Parent.Name == item2.Name);
  123. Each(rot, a => item2.AddChild(a));
  124. }
  125.  
  126. if(item.Name == "SG 1")
  127. {
  128. var last = treeData.Rotation.Where(w => w.Name == "Rot 1").SingleOrDefault();
  129. item.AddChild(last);
  130. }
  131. }
  132.  
  133.  
  134. Console.ReadKey();
  135. }
  136.  
  137. private static void Each<T>(IEnumerable<T> items, Action<T> action)
  138. {
  139. foreach (var item in items)
  140. {
  141. action(item);
  142. }
  143. }
  144. }
  145.  
  146. public class TreeNode : ITreeNode
  147. {
  148. public ObservableCollection<ITreeNode> Children { get; set; }
  149. public string Name { get; set; }
  150. public ITreeNode Parent { get; set; }
  151.  
  152. public TreeNode()
  153. {
  154. Children = new ObservableCollection<ITreeNode>();
  155. }
  156.  
  157. public void AddChild(ITreeNode child)
  158. {
  159. Children.Add(child);
  160. }
  161.  
  162. public void RemoveChild(ITreeNode child)
  163. {
  164. Children.Remove(child);
  165. }
  166. }
  167.  
  168. public class DiscountGroup : TreeNode
  169. {
  170. public int ID { get; set; }
  171. }
  172.  
  173. public class Rotation : TreeNode
  174. {
  175. public int ID { get; set; }
  176. }
  177.  
  178. public class PriceIntervals : TreeNode
  179. {
  180. public int ID { get; set; }
  181. }
  182.  
  183. public class SubGroup : TreeNode
  184. {
  185. public int ID { get; set; }
  186. }
  187.  
  188. public class GroupedTreeBranches
  189. {
  190. public IEnumerable<DiscountGroup> DiscountGroup { get; set; }
  191. public IEnumerable<Rotation> Rotation { get; set; }
  192. public IEnumerable<PriceIntervals> PriceIntervals { get; set; }
  193. public IEnumerable<SubGroup> SubGroup { get; set; }
  194. }
  195.  
  196. public interface ITreeNode
  197. {
  198. ObservableCollection<ITreeNode> Children { get; set; }
  199. string Name { get; set; }
  200. ITreeNode Parent { get; set; }
  201. void AddChild(ITreeNode child);
  202. void RemoveChild(ITreeNode child);
  203. }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement