| 1234567891011121314151617181920212223242526272829303132 |
- using System.Collections.Generic;
- using System.Text;
- namespace System.Common
- {
- public static class Tree
- {
- /// <summary>
- /// 递归算法
- /// </summary>
- /// <param name="data"></param>
- /// <param name="parentId"></param>
- /// <returns></returns>
- public static List<TreeModel> TreeRecursion(this List<TreeModel> data, int parentId = 0)
- {
- List<TreeModel> newList = new List<TreeModel>();
- List<TreeModel> 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;
- }
- }
- }
|