コード例 #1
0
ファイル: CategoryService.cs プロジェクト: linkypi/VSMS
        /// <summary>
        /// 获取所有类别    类别中包含所有子类别
        /// </summary>
        /// <returns></returns>
        public List <Category> GetModels()
        {
            List <Category> list = cdao.GetModels();

            if (list == null)
            {
                return(null);
            }
            Category        currentCg = list[0];
            List <Category> retList   = new List <Category>();
            Category        parent    = null;
            Category        child     = null;
            int             index     = 0;

            foreach (Category cg in list)
            {
                index++;
                if (cg.CID == cg.PCID)
                {
                    if (index == 1)
                    {
                        parent = new Category();
                        parent = cg; continue;
                    }
                    retList.Add(parent);
                    currentCg = cg;
                    parent    = new Category();
                    parent    = cg; continue;
                }
                if (cg.PCID == currentCg.PCID)
                {
                    child = new Category();
                    child = cg;
                    parent.Children.Add(child);
                }
            }
            retList.Add(parent);
            return(retList);
        }