继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

luckyer
关注TA
已关注
手记 9
粉丝 10
获赞 62
 List<table> ListInfo =getOrderList( data.ToList());//

 public List<table> getOrderList(List<table> data)
        {
           table temp = new table();
           List<table> tempLeafList = new List<table>();
           List<table> tempList = new List<table>();
           List<table> resultList = new List<table>();//接收有序结果的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<table > getLeafList(List<table > tempList, List<table > data, List<table > resultList)
        {
            table temp = new table ();
            List<table > tempLeafList = new List<table >();
            List<table > temp2List = new List<table >();
            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 记下来给自己呀^_^

打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP