C# List 转 Tree 公共方法

# 用C# 写了个List数据结构转树形数据结构的公共扩展方法

        /// <summary>
        /// 将列表转换为树形结构
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="list">数据</param>
        /// <param name="rootwhere">根条件</param>
        /// <param name="childswhere">节点条件</param>
        /// <param name="addchilds">添加子节点</param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static List<T> ToTree<T>(this List<T> list, Func<T, T, bool> rootwhere, Func<T, T, bool> childswhere, Action<T, IEnumerable<T>> addchilds, T entity = default(T))
        {
            var treelist = new List<T>();
            //空树
            if (list == null || list.Count == 0)
            {
                return treelist;
            }
            if (!list.Any<T>(e => rootwhere(entity, e)))
            {
                return treelist;
            }

            //树根
            if (list.Any<T>(e => rootwhere(entity, e)))
            {
                treelist.AddRange(list.Where(e => rootwhere(entity, e)));
            }

            //树叶
            foreach (var item in treelist)
            {
                if (list.Any(e => childswhere(item, e)))
                {
                    var nodedata = list.Where(e => childswhere(item, e)).ToList();
                    foreach (var child in nodedata)
                    {
                        //添加子集
                        var data = list.ToTree(childswhere, childswhere, addchilds, child);
                        addchilds(child, data);
                    }
                    addchilds(item, nodedata);
                }
            }

            return treelist;
        }

# 方法说明:

第一个参数:根节点的条件
第二个参数:根节点和子节点的关系(注意,这地方如果条件不充分,会导致异常,无限递归)
第三个参数:当前数据添加子集

调用示例1、(id--->pid)

var treedata = list.ToTree<TTest>((r, c) =>
                 {
                     return c.Pid == 0;
                 },
                (r, c) =>
                {
                    return r.Id == c.Pid;
                },
                (r, datalist) =>
                {
                    r.Childs.AddRange(datalist);
                });

调用示例2、(LevelCode)

treedata2 = liststr2.ToTree((r, c) => c.LevelCode.Length == 6, (r, c) =>
                {
                    if (r == null || c == null)
                    {
                        return false;
                    }

                    if ((r.LevelCode.Length + 6 == c.LevelCode.Length) &&
                        r.LevelCode == c.LevelCode.Substring(0, r.LevelCode.Length))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                },
                (r, datalist) => { r.Childs.AddRange(datalist); });

C# List 转 Tree 公共方法

上一篇:VMware Windows Server 2008 R2 X64 虚拟机安装教程


下一篇:Android:属性scaleType与图片的显示