using System.Collections.Generic; using System.Text; namespace System.Common { public static class Tree { /// /// 递归算法 /// /// /// /// public static List TreeRecursion(this List data, int parentId = 0) { List newList = new List(); List item = data.FindAll(t => t.parentid == parentId);//data建议在调用此扩展方法前已经排序过 if (item.Count > 0) { foreach (TreeModel entity in item) { var _list= TreeRecursion(data, entity.id); entity.children = _list.Count>0?_list:null; newList.Add(entity); } } return newList; } } }