分 享

【凤凰社原创】.NET树形结构帮助类


以下是.NET树形结构帮助类的源码,可以拿去直接用,有兴趣的可以参考一下



using System;

using System.Collections.Generic;

using System.Linq;

 

namespace FHCollege.Util

{

    /// <summary>

    /// 树结构帮助类

    /// </summary>

    public class TreeHelper

    {

        /// <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">树模型(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.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);

        }

    }

}


转载请注明出处!

有不明白的可以在本帖下方评论,我会及时回复




0 评论

回复