/// <summary> /// 树结构帮助类 /// </summary> public class TreeHelper { #region 外部接口 /// <summary> /// 建造树结构 /// </summary> /// <param name="allNodes">所有的节点</param> /// <returns></returns> public static List<T> BuildTree<T>(List<T> allNodes) where T : TreeModel, new() { List<T> resData = new List<T>(); var rootNodes = allNodes.Where(x => x.ParentId == "0" || x.ParentId.IsNullOrEmpty()).ToList(); resData = rootNodes; resData.ForEach(aRootNode => { if (HaveChildren(allNodes, aRootNode.Id)) aRootNode.Children = GetChildren(allNodes, aRootNode); }); return resData; } /// <summary> /// 获取所有子节点 /// 注:包括自己 /// </summary> /// <typeparam name="T">节点类型</typeparam> /// <param name="allNodes">所有节点</param> /// <param name="parentNode">父节点</param> /// <param name="includeSelf">是否包括自己</param> /// <returns></returns> public static List<T> GetChildren<T>(List<T> allNodes, T parentNode, bool includeSelf) where T : TreeModel { List<T> resList = new List<T>(); if (includeSelf) resList.Add(parentNode); _getChildren(allNodes, parentNode, resList); return resList; void _getChildren(List<T> _allNodes, T _parentNode, List<T> _resNodes) { var children = _allNodes.Where(x => x.ParentId == _parentNode.Id).ToList(); _resNodes.AddRange(children); children.ForEach(aChild => { _getChildren(_allNodes, aChild, _resNodes); }); } } #endregion #region 私有成员 /// <summary> /// 获取所有子节点 /// </summary> /// <typeparam name="T">树模型(TreeModel或继承它的模型)</typeparam> /// <param name="nodes">所有节点列表</param> /// <param name="parentNode">父节点Id</param> /// <returns></returns> private static List<object> GetChildren<T>(List<T> nodes, T parentNode) where T : TreeModel, new() { Type type = typeof(T); var properties = type.GetProperties().ToList(); List<object> resData = new List<object>(); var children = nodes.Where(x => x.ParentId == parentNode.Id).ToList(); children.ForEach(aChildren => { T newNode = new T(); resData.Add(newNode); //赋值属性 properties.Where(x => x.CanWrite).ForEach(aProperty => { var value = aProperty.GetValue(aChildren, null); aProperty.SetValue(newNode, value); }); //设置深度 newNode.Level = parentNode.Level + 1; if (HaveChildren(nodes, aChildren.Id)) newNode.Children = GetChildren(nodes, newNode); }); return resData; } /// <summary> /// 判断当前节点是否有子节点 /// </summary> /// <typeparam name="T">树模型</typeparam> /// <param name="nodes">所有节点</param> /// <param name="nodeId">当前节点Id</param> /// <returns></returns> private static bool HaveChildren<T>(List<T> nodes, string nodeId) where T : TreeModel, new() { return nodes.Exists(x => x.ParentId == nodeId); } #endregion }
扩展方法:
public static partial class Extention { /// <summary> /// 判断是否为Null或者空 /// </summary> /// <param name="obj">对象</param> /// <returns></returns> public static bool IsNullOrEmpty(this object obj) { if (obj == null) return true; else { string objStr = obj.ToString(); return string.IsNullOrEmpty(objStr); } } /// <summary> /// 给IEnumerable拓展ForEach方法 /// </summary> /// <typeparam name="T">模型类</typeparam> /// <param name="iEnumberable">数据源</param> /// <param name="func">方法</param> public static void ForEach<T>(this IEnumerable<T> iEnumberable, Action<T> func) { foreach (var item in iEnumberable) { func(item); } } }