1.声明树结构model类

public class TreeModel{public string ID { set; get; }public string PARENTID { set; get; }public string NAME { set; get; }public List<TreeModel> TREECHILDREN { set; get; }}

2.利用反射将泛型list转换为树形结构类

/// <summary>/// 公用递归(反射转换List)/// </summary>/// <typeparam name="T"></typeparam>/// <param name="allList">数据列表</param>/// <param name="parentId">父级ID</param>/// <param name="idField">id字段名称</param>/// <param name="parentIdField">父级id字段名称</param>/// <param name="nameField">name字段名称</param>/// <returns></returns>public List<TreeModel> ConversionList<T>(List<T> allList, string parentId, string idField, string parentIdField, string nameField){List<TreeModel> list = new List<TreeModel>();TreeModel model = null;foreach (var item in allList){model = new TreeModel();foreach (System.Reflection.PropertyInfo p in item.GetType().GetProperties()){if (p.Name == idField){model.ID = p.GetValue(item)?.ToString();//判空}if (p.Name == parentIdField){model.PARENTID = p.GetValue(item)?.ToString();//判空}if (p.Name == nameField){model.NAME = p.GetValue(item)?.ToString();//判空}}list.Add(model);}return OperationParentData(list, parentId);}

3.递归

/// <summary>/// 公用递归(处理递归最父级数据)/// </summary>/// <param name="treeDataList">树形列表数据</param>/// <param name="parentId">父级Id</param>/// <returns></returns>public List<TreeModel> OperationParentData(List<TreeModel> treeDataList, string parentId){var data = treeDataList.Where(x => x.PARENTID == parentId);List<TreeModel> list = new List<TreeModel>();foreach (var item in data){OperationChildData(treeDataList, item);list.Add(item);}return list;}/// <summary>/// 公用递归(递归子级数据)/// </summary>/// <param name="treeDataList">树形列表数据</param>/// <param name="parentItem">父级model</param>public void OperationChildData(List<TreeModel> treeDataList, TreeModel parentItem){var subItems = treeDataList.Where(ee => ee.PARENTID == parentItem.ID).ToList();if (subItems.Count != 0){parentItem.TREECHILDREN = new List<TreeModel>();parentItem.TREECHILDREN.AddRange(subItems);foreach (var subItem in subItems){OperationChildData(treeDataList, subItem);}}}
/// <summary>/// 公用递归(递归子级数据)/// </summary>/// <param name="treeDataList">树形列表数据</param>/// <param name="parentItem">父级model</param>public void OperationChildData(List<TreeModel> treeDataList, TreeModel parentItem){var subItems = treeDataList.Where(ee => ee.PARENTID == parentItem.ID).ToList();if (subItems.Count != 0){parentItem.TREECHILDREN = new List<TreeModel>();parentItem.TREECHILDREN.AddRange(subItems);foreach (var subItem in subItems){OperationChildData(treeDataList, subItem);}}}

示例:

C# 递归实现tree view(树结构)相关推荐

  1. Angular实现递归指令 - Tree View

    在层次数据结构展示中,树是一种极其常见的展现方式.比如系统中目录结构.企业组织结构.电子商务产品分类都是常见的树形结构数据. 这里我们采用Angular的方式来实现这类常见的tree view结构. ...

  2. JavaFX UI控件教程(十四)之Tree View

    翻译自   Tree View 在本章中,您将学习如何在JavaFX应用程序中构建树结构,向树视图添加项,处理事件以及通过实现和应用单元工厂来自定义树单元. 包的TreeView类javafx.sce ...

  3. (转)用Ajax技术让IE Web Control Tree View实现大数据量读取

    转自:http://www.cnblogs.com/dingsea/archive/2005/10/26/262220.html 相信不少人都用过微软提供的一款控件: IEControl, 其中的Tr ...

  4. USB HID 多设备检查工具,使用USB Tree View源码修改而来

    最近一直在研究USB HID多设备升级,然后就查看了微软提供的WDK USB Tree View源码,但是没有找到USB多设备端口绑定的决定因素,当在电脑接上多个相同的USB设备时,就不好判断USB ...

  5. MPT(Merkle Patricia Tree)树结构示例

    MPT(Merkle Patricia Tree): 参考深入浅出以太坊MPT(Merkle Patricia Tree) MPT树结构示例1: rootHash扩展节点 prefix 0 prefi ...

  6. linux tree命令 树结构显示目录文件

    命令简介 tree命令的中文意思为"树",功能是以树形结构列出指定目录下的所有内容,包括所有文件.子目录及子目录里的目录和文件. 命令格式 tree [option] [direc ...

  7. tree view 後台編寫

    因為我要與數據庫聯系起來,所以必須後台編寫: 'tree 綁定     Private Sub Loadtree()Sub Loadtree()         Dim myda As SqlData ...

  8. visual tree view在工具箱力没有_Visual-Inertial Odometry

    1. 定义 "Visual-Inertial Odometry",俗称VIO,是一个使用一个或者多个相机.一个或者多个IMU(Inertial Measurement Units) ...

  9. 简单的无限分级类 category, menus, jquery tree view

    1. 简单的无限分级类 category, menus <?phpmysql_connect("localhost","root","root& ...

  10. tree view android,AndroidTreeView

    特点 N-级 可展开与收缩的树 自定义值,view,节点样式 横竖屏切换可保持展开(或者关闭)状态 支持节点的选择模式 动态删除或者添加节点. 使用 添加依赖:compile 'com.github. ...

最新文章

  1. 怎么修改网页服务器数据库连接,如何修改网页服务器数据库连接
  2. 从脚本本身获取Bash脚本的源目录
  3. 利用NLTK进行分句分词
  4. python 全栈开发,Day66(web应用,http协议简介,web框架)
  5. 对南昌杀人案的一些看法
  6. Spring的异步方法
  7. 黑马程序员——11 正则表达式
  8. 小程序是否转发群还是个人(转发功能)
  9. 【[USACO13NOV]没有找零No Change】
  10. 推荐21个顶级的Vue UI库!
  11. Java连接mysql数据库的方式_java连接MySQL数据库的方式
  12. easyui框架和显示(一)
  13. Vue2在使用Element-UI的分页组件遇到的问题(如PageSize,PageNum等)
  14. 集合和数组区别_了解您的编码基础:集合和数组之间的主要区别
  15. 牵线搭桥,『桥接模式』
  16. 指甲之怎样以指甲测健康
  17. X-Powered-By: Servlet/3.0漏洞修复
  18. Python中10个常用的内置函数
  19. javascript程序员简历模板
  20. ArcGIS|一文彻底搞懂GIS图斑编号

热门文章

  1. 求大于3的素数c语言,c语言判断一个大于3的数是否是素数
  2. 3803. 数组去重-AcWing题库
  3. spark sql 入门详解
  4. 让程序员爱不释手的两个导航网站
  5. 牛客练习赛26---未完
  6. 乐学计算机,乐学高考电脑版
  7. C语言中使输入的字符串反序输出,C语言: 写一函数,使输入的一个字符串按反序存放,在主函数中输入和输出字符串。...
  8. er图一对多的关系怎么体现_抠图高级技法之混合剪切篇
  9. linux 解决Linux上解压jdk报错gzip: stdin: not in gzip format
  10. Redis-槽道原理