手记

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

 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 记下来给自己呀^_^

2人推荐
随时随地看视频
慕课网APP