栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

递归 小记(当时是为了按树结构排序,让子节点在其父节点下排下来)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

递归  小记(当时是为了按树结构排序,让子节点在其父节点下排下来)

 List ListInfo =getOrderList( data.ToList());//

 public List
getOrderList(List
data) { table temp = new table(); List
tempLeafList = new List
(); List
tempList = new List
(); List
resultList = new List
();//接收有序结果的List temp = data.SingleOrDefault(a => a.parentId == 0);//找到根节点 resultList.Add(temp);根节点为第一条数据 tempLeafList = data.Where(表达式).ToList();//找到所有的叶子节点 resultList.AddRange(tempLeafList); tempList = data.Where(表达式).ToList();//找到分支节点(分支节点就需要继续往下找了) resultList = getLeafList(tempList, data, resultList);//去找分支节点下的数据 return resultList; } public List
getLeafList(List
tempList, List
data, List
resultList) { table temp = new table (); List
tempLeafList = new List
(); List
temp2List = new List
(); for (int i = 0; i < tempList.Count; i++) { temp = tempList[i]; resultList.Add(temp); tempLeafList = data.Where(a => a.parentId == temp.ID&&).ToList();//叶子节点 resultList.AddRange(tempLeafList); temp2List = data.Where(a => a.parentId == temp.ID&&).ToList();//分支节点 getLeafList(temp2List, data, resultList); } return resultList; }

宝宝第一次用递归T-T 记下来给自己呀^_^

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号