有關目錄樹的操作

        /// <summary>
        /// 取得所有子分類ID
        /// </summary>
        /// <param name="categoryid">要取得的根分類ID</param>
        /// <param name="result">結果列表</param>
        public static void GetAllSubCatIds(int categoryid, List<int> result)
        {
            foreach (category item in category.Find(c => c.parentid == categoryid))
            {
                result.Add(item.id);
                GetAllSubCatIds(item.id, result);
            }
        }

        //查出category中所有的父節點

        public static List<ECategory> getNavList(int categoryid)
        {
            List<ECategory> navList = new List<ECategory>();
            category cate1 = category.SingleOrDefault(p => p.id == categoryid);
            int parentid = cate1.parentid;
            int cid = cate1.id;
            while (parentid >= 0)
            {
                category cate = category.SingleOrDefault(p => p.id == cid);
                ECategory model = new ECategory();
                model.id = cate.id;
                model.name = cate.name;
                model.orderid = cate.orderid;
                model.parentid = parentid;
                navList.Add(model);
                if (parentid != 0)
                {
                    category cate2 = category.SingleOrDefault(p => p.id == cate.parentid);
                    parentid = cate2.parentid;
                    cid = cate.parentid;
                }
                else
                    break;
            }
            navList.Reverse();
            return navList;
        }

 //如果是article頁面,獲得他父節點在category中的id
        public static int getid(int id)
        {
            article article = article.SingleOrDefault(p => p.id == id);
            int categoryid = article.categoryid;
            return categoryid;
        }

        //找到所在id的order==2父節點
        public static int getfather(int id)
        {
            category category1 = category.SingleOrDefault(p => p.id == id);
            int order = category1.orderid;
            int parent = category1.parentid;
            int fatherid = id;
            while (order >= 2)
            {
                category categ = category.SingleOrDefault(p => p.id == parent);
                order = categ.orderid;
                parent = categ.parentid;
                fatherid = categ.id;
            }
            return fatherid;
        }

        //獲得當前id的1級導航
        public static int getfirst(int id)
        {
            category categ = category.SingleOrDefault(p => p.id == id);
            int firstid = id;
            if (categ.orderid != 1)
            {
                firstid = getfather(id);
                category cate = category.SingleOrDefault(p => p.id == firstid);
                if (cate.orderid != 1)
                { firstid = cate.parentid; }
            }
            return firstid;
        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章