快速导航
递归 小记(当时是为了按树结构排序,让子节点在其父节点下排下来)
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 记下来给自己呀^_^