有关目录树的操作

3 篇文章 0 订阅
1 篇文章 0 订阅
        /// <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;
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值