有关目录树的操作
来源:互联网 发布:mac怎样安装搜狗输入法 编辑:程序博客网 时间:2024/06/16 19:39
/// <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; }