internal void AddChild(ProductTypeNode node) { Contract.Requires (node.Parent == null); node.Parent = this; this.TypeChildren.Add (node); }
private IEnumerable<ProductTypeNode> LoadProductTypesHierarchy() { string cmdText = @" SELECT * FROM ProductType AS PT ORDER BY PT.[parent_id]"; List<ProductTypeNode> nodes = new List<ProductTypeNode> (); Action<SqlDataReader> fetcher = reader => { ProductTypeOridinal pto = new ProductTypeOridinal (reader); pto.LoadOrdinals (); while (reader.Read ()) { ProductTypeValue ptv = pto.GetProductTypeValue (); ProductTypeNode node = new ProductTypeNode (); node.ProductTypeValue = ptv; nodes.Add (node); } }; ExecuteQuery (cmdText, null, fetcher); var lookup = nodes.ToLookup(ptv => ptv.ProductTypeValue.ParentType); nodes.Clear (); var parents = lookup[(ProductType?)null]; nodes.AddRange (parents); foreach (var item in lookup) { if (!item.Key.HasValue) continue; ProductTypeNode parent = nodes.FirstOrDefault (ptn => ptn.ProductTypeValue.ProductType == item.Key.Value); if (parent == null) continue; parent.AddRange (lookup[item.Key]); } return nodes; }