/// <summary> /// get directories /// </summary> /// <returns></returns> public IList <PartsModel> GetDirectory() { using (var dao = new PartsDao(_dbName)) { return(dao.GetDirectory()); } }
/// <summary> /// get a parts and directories tree (root node) /// </summary> /// <returns>root node</returns> public PartsModel BuildPartsTree() { IList <PartsModel> directories = null; IList <PartsModel> parts = null; using (var dao = new PartsDao(_dbName)) { directories = dao.GetDirectory(); parts = dao.GetParts(); } var dictNodes = directories.ToDictionary(m => m.Id, m => m); var root = new PartsModel() { Name = _dbName, Id = 0, PId = 0, SortPos = 0, PartsType = PartsModel.Type.ROOT, }; // build directory tree foreach (var node in dictNodes) { var pid = node.Value.PId; if (pid == 0) { root.Children.Add(node.Value); node.Value.Parent = root; } else { if (dictNodes.ContainsKey(pid)) { var parentItem = dictNodes[pid]; parentItem.Children.Add(node.Value); node.Value.Parent = parentItem; } } } // build part tree under the directory foreach (var part in parts) { if (dictNodes.ContainsKey(part.PId)) { var parentItem = dictNodes[part.PId]; parentItem.Children.Add(part); part.Parent = parentItem; } } return(root); }