コード例 #1
0
 /// <summary>
 /// get parts
 /// </summary>
 /// <returns></returns>
 public IList <PartsModel> GetParts()
 {
     using (var dao = new PartsDao(_dbName))
     {
         return(dao.GetParts());
     }
 }
コード例 #2
0
        /// <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);
        }