Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- namespace Console472
- {
- class Program
- {
- static void Main(string[] args)
- {
- var treeData = new GroupedTreeBranches()
- {
- DiscountGroup = new List<DiscountGroup>()
- {
- new DiscountGroup
- {
- Name = "DG1",
- ID = 1
- },
- new DiscountGroup
- {
- Name = "DG2",
- ID = 2
- },
- new DiscountGroup
- {
- Name = "DG3",
- ID = 3
- }
- },
- SubGroup = new List<SubGroup>()
- {
- new SubGroup
- {
- Name = "SG 1",
- ID = 200,
- Parent = new DiscountGroup(){ID = 1, Name = "DG1"}
- },
- new SubGroup
- {
- Name = "SG 2",
- ID = 201,
- Parent = new DiscountGroup(){ID = 1, Name = "DG1"}
- },
- new SubGroup
- {
- Name = "SG 3",
- ID = 202,
- Parent = new DiscountGroup(){ID = 2, Name = "DG2"}
- },
- new SubGroup
- {
- Name = "SG 4",
- ID = 203,
- Parent = new DiscountGroup(){ID = 2, Name = "DG2"}
- }
- },
- PriceIntervals = new List<PriceIntervals>()
- {
- new PriceIntervals
- {
- Name = "Price 1",
- ID = 100,
- Parent = new DiscountGroup { ID = 1, Name = "DG1"}
- },
- new PriceIntervals
- {
- Name = "Price 2",
- Parent = new DiscountGroup { ID = 1, Name = "DG1"}
- },
- new PriceIntervals
- {
- Name = "Price 2",
- ID = 101,
- Parent = new DiscountGroup { ID = 2, Name = "DG2"}
- },
- },
- Rotation = new List<Rotation>()
- {
- new Rotation
- {
- Name = "Rot 1",
- ID = 20,
- Parent = new DiscountGroup(){ID = 200, Name = "SG 1"},
- },
- new Rotation
- {
- Name = "Rot 2",
- ID = 21,
- Parent = new SubGroup(){ID = 200, Name = "SG 1"},
- },
- new Rotation
- {
- Name = "Rot 3",
- ID = 22,
- Parent = new SubGroup(){ID = 201, Name = "SG 2"},
- },
- new Rotation
- {
- Name = "Rot 4",
- ID = 23,
- Parent = new SubGroup(){ID = 201, Name = "SG 2"},
- },
- }
- };
- var tn = new TreeNode();
- foreach (var item in treeData.DiscountGroup)
- {
- tn.AddChild(item);
- var sg = treeData.SubGroup.Where(w => w.Parent.Name == item.Name);
- Each(sg, a => item.AddChild(a));
- var pi = treeData.PriceIntervals.Where(w => w.Parent.Name == item.Name);
- Each(pi, a => item.AddChild(a));
- foreach (var item2 in item.Children.Where(w => w.GetType() == typeof(SubGroup)))
- {
- var rot = treeData.Rotation.Where(w => w.Parent.GetType() == typeof(SubGroup) && w.Parent.Name == item2.Name);
- Each(rot, a => item2.AddChild(a));
- }
- if(item.Name == "SG 1")
- {
- var last = treeData.Rotation.Where(w => w.Name == "Rot 1").SingleOrDefault();
- item.AddChild(last);
- }
- }
- Console.ReadKey();
- }
- private static void Each<T>(IEnumerable<T> items, Action<T> action)
- {
- foreach (var item in items)
- {
- action(item);
- }
- }
- }
- public class TreeNode : ITreeNode
- {
- public ObservableCollection<ITreeNode> Children { get; set; }
- public string Name { get; set; }
- public ITreeNode Parent { get; set; }
- public TreeNode()
- {
- Children = new ObservableCollection<ITreeNode>();
- }
- public void AddChild(ITreeNode child)
- {
- Children.Add(child);
- }
- public void RemoveChild(ITreeNode child)
- {
- Children.Remove(child);
- }
- }
- public class DiscountGroup : TreeNode
- {
- public int ID { get; set; }
- }
- public class Rotation : TreeNode
- {
- public int ID { get; set; }
- }
- public class PriceIntervals : TreeNode
- {
- public int ID { get; set; }
- }
- public class SubGroup : TreeNode
- {
- public int ID { get; set; }
- }
- public class GroupedTreeBranches
- {
- public IEnumerable<DiscountGroup> DiscountGroup { get; set; }
- public IEnumerable<Rotation> Rotation { get; set; }
- public IEnumerable<PriceIntervals> PriceIntervals { get; set; }
- public IEnumerable<SubGroup> SubGroup { get; set; }
- }
- public interface ITreeNode
- {
- ObservableCollection<ITreeNode> Children { get; set; }
- string Name { get; set; }
- ITreeNode Parent { get; set; }
- void AddChild(ITreeNode child);
- void RemoveChild(ITreeNode child);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement