c# 递归树型结构
/// <summary> /// 分类树形结构 http://192.168.2.177:1222/api/classification/GetclassificationTree /// </summary> /// <returns></returns> [Route("api/classification/GetclassificationTree")] [HttpGet] public string GetclassificationTree() { WebApplication.Controllers.TreeMethod tm = new TreeMethod(); // 找到所有的父节点 List<TreeEntity> treeList1 = tm.findAllParents(); if (treeList1 != null) { for (int i = 0; i < treeList1.Count; i++) { TreeEntity tree = treeList1[i]; // 拼接父节点 //result += "|--" + tree.name; result += "{id:"+tree.id+ ",pId:"+tree.pid+",name:'"+tree.name+"'},"; // 绑定孩子 ChildResult = tm.BindChildByParent(tree.id, "").TrimEnd(','); temp = result + ChildResult; } } else { temp += "没有数据!"; } result = "["+ temp.TrimEnd(',')+ "]"; return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "获取数据成功",Data= temp }); } } public class TreeEntity { public string id { get; set; } public string name { get; set; } public string pid { get; set; } } internal class TreeMethod { /// <summary> /// 找到所有的父节点 /// </summary> /// <returns></returns> public List<TreeEntity> findAllParents() { List<TreeEntity> treeList = new List<TreeEntity>(); using (var db = SugarDao.GetInstance()) { var list = db.Queryable<tb_classification>().Where(it => it.pid == 0 && it.status==1).ToList(); if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { TreeEntity myTree = new TreeEntity(); myTree.id = list[i].id.ToString(); myTree.name = list[i].title; myTree.pid = list[i].pid.ToString(); treeList.Add(myTree); } } } return treeList; } /// <summary> /// 根据父节点找到所有的子节点 /// </summary> /// <param name="pid"></param> /// <returns></returns> public List<TreeEntity> findChildByPid(string pid) { int p_id = Convert.ToInt32(pid); List<TreeEntity> treeList = new List<TreeEntity>(); using (var db = SugarDao.GetInstance()) { var list = db.Queryable<tb_classification>().Where(it => it.pid == p_id&&it.status==1).ToList(); if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { TreeEntity myTree = new TreeEntity(); myTree.id = list[i].id.ToString(); myTree.name = list[i].title; myTree.pid = list[i].pid.ToString(); treeList.Add(myTree); } } } return treeList; } /// <summary> /// 查看是否存在子节点 /// </summary> /// <param name="pid"></param> /// <returns></returns> public bool HasChild(string pid) { int p_id = Convert.ToInt32(pid); int count = 0; bool flag = false; using (var db = SugarDao.GetInstance()) { var list = db.Queryable<tb_classification>().Where(it => it.pid == p_id&it.status==1).ToList(); for (int i = 0; i < list.Count; i++) { count++; } if (count > 0) { flag = true; } } return flag; } string Tree = string.Empty; /// <summary> /// 使用递归拼接父节点的子节点 /// </summary> /// <param name="pid"></param> /// <param name="prefix"></param> public string BindChildByParent(string pid, string prefix) { if (this.HasChild(pid)) { // 得到当前父节点下的所有孩子 List<TreeEntity> list = this.findChildByPid(pid); // 循环拼接当前父节点下的孩子 for (int i = 0; i < list.Count; i++) { //Tree += "|----" + prefix + list[i].name; Tree += "{id:" + list[i].id + ",pId:" + list[i].pid + ",name:'" + list[i].name + "'},"; if (this.HasChild(list[i].id)) { this.BindChildByParent(list[i].id, "--"); } } } return Tree; } }
最终效果:
"{\"Result\":1,\"Msg\":\"获取数据成功\",\"Data\":\"{id:1,pId:0,name:\\u0027新闻\\u0027},{id:16,pId:0,name:\\u0027蔬菜\\u0027},{id:2,pId:1,name:\\u0027娱乐新闻\\u0027},{id:20,pId:2,name:\\u0027新增新闻分类\\u0027},{id:23,pId:20,name:\\u0027新增新闻分类2\\u0027},{id:26,pId:23,name:\\u0027新增分类3\\u0027},{id:7,pId:1,name:\\u0027时政新闻\\u0027},{id:8,pId:1,name:\\u0027科教文卫\\u0027},{id:9,pId:1,name:\\u0027城建公交\\u0027},{id:10,pId:1,name:\\u0027经济新闻\\u0027},{id:11,pId:1,name:\\u0027政法新闻\\u0027},{id:12,pId:1,name:\\u0027社会新闻\\u0027},{id:13,pId:1,name:\\u0027体育新闻\\u0027},{id:14,pId:1,name:\\u0027军事新闻\\u0027},{id:15,pId:1,name:\\u0027国际新闻\\u0027},{id:18,pId:16,name:\\u0027白菜\\u0027},{id:19,pId:16,name:\\u0027萝卜\\u0027},\"}"