zoukankan      html  css  js  c++  java
  • 生成树状结构

    using cpf360.Common;
    using cpf360.ModelInfo;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace cpf360.BLL
    {
        public class TreeService<T> where T : TreeModel<T>
        {
            public static List<T> TreeLoop(List<T> datas)
            {
                return TreeLoop(datas, datas);
            }
    
            public static List<T> TreeLoop(List<T> basedatas, List<T> datas, long parentId = 0, string pathId = "0/", string pathName = "Root/")
            {
                var result = datas.Where(tm => tm.ParentId == parentId).ToList();
                result.ForEach(item =>
                {
                    item.PathId = pathId.IsNullOrEmpty() ? "{0}/".FormatString(item.Id) : "{0}{1}/".FormatString(pathId, item.Id);
                    item.PathName = pathName.IsNullOrEmpty() ? "{0}/".FormatString(item.Title) : "{0}{1}/".FormatString(pathName, item.Title);
                    item.Childrens = basedatas.Where(tm => tm.ParentId == item.Id).ToList();
                    item.HasChildrens = item.Childrens.Count > 0;
                    if (item.HasChildrens)
                    {
                        TreeLoop(basedatas, item.Childrens, item.Id, item.PathId, item.PathName);
                    }
                });
                return result;
            }
        }
    }
  • 相关阅读:
    iOS崩溃报告获取一
    GCDTimer
    Runtime
    Socket
    冒泡排序笔记
    学习java虚拟机笔记
    ftp发送文件包括中文名
    java email
    批量数据插入高效 转发
    读取本地硬盘文件,快速扫描插入 数据库
  • 原文地址:https://www.cnblogs.com/chenyishi/p/8794232.html
Copyright © 2011-2022 走看看