using System.Collections.Generic;
using System.Text;

namespace ZXDT.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;
        }
    }
}
