C# 实现Tree,包含parentId和children
1.先定义一个类型
public class Node
{
[JsonProperty(PropertyName = "id", NullValueHandling = NullValueHandling.Ignore)]
public string id { get; set; } [JsonProperty(PropertyName = "text", NullValueHandling = NullValueHandling.Ignore)]
public string Text { get; set; } [JsonProperty(PropertyName = "checked", NullValueHandling = NullValueHandling.Ignore)]
public bool Checked { get; set; } [JsonProperty(PropertyName = "children", NullValueHandling = NullValueHandling.Ignore)]
public IList<Node> Children { get; set; } [JsonProperty(PropertyName = "parentId", NullValueHandling = NullValueHandling.Ignore)]
public string ParentId { get; set; }
}
2.数据源,我们从数据库查出来一般是以下这样的数据
IList<Node> treeList = new List<Node>();
treeList.Add(new Node { Id = "f31a347e4be70da6d925bfaddf0e896b", Text = "商务经典", ParentId = "" });
treeList.Add(new Node { Id = "b50d381e694c0227242ff7b55685178c", Text = "LZ01", ParentId = "f31a347e4be70da6d925bfaddf0e896b" }); treeList.Add(new Node { Id = "a921c809276dadf00bd45dd527564f02", Text = "休闲时光", ParentId = "" });
treeList.Add(new Node { Id = "1bf52435e4f0af478dc9137ca7719fbb", Text = "XX01", ParentId = "a921c809276dadf00bd45dd527564f02" }); treeList.Add(new Node { Id = "ee43cc1ceb57a3793c5c11d6d632fd22", Text = "摩登时代", ParentId = "" });
treeList.Add(new Node { Id = "fb9a268c6061d962dbb5fc5f55c803f8", Text = "MD01", ParentId = "ee43cc1ceb57a3793c5c11d6d632fd22" });
3.递归初始化树
/// <summary>
/// 递归初始化树
/// </summary>
/// <param name="nodes">结果</param>
/// <param name="parentID">父ID</param>
/// <param name="sources">数据源</param>
private void InitTree(IList<Node> nodes, string parentID, IList<Node> sources)
{
Node tempNode;
//递归寻找子节点
var tempTree = sources.Where(item => item.ParentId == parentID).ToList();
foreach (Node row in tempTree)
{
tempNode = new Node()
{
Id = row.Id,
Text = row.Text,
ParentId = row.ParentId,
Children = new List<Node>()
};
nodes.Add(tempNode);
InitTree(tempNode.Children, row.Id, sources);
}
}
4.调用得到结果
var tree = new List<Node>();
InitTree(tree, "", treeList);
string json = JsonConvert.SerializeObject(tree); //得到结果:[{"id":"f31a347e4be70da6d925bfaddf0e896b","text":"商务经典","checked":false,"children":[{"id":"b50d381e694c0227242ff7b55685178c","text":"LZ01","checked":false,"children":[],"parentId":"f31a347e4be70da6d925bfaddf0e896b"}],"parentId":"0"},{"id":"a921c809276dadf00bd45dd527564f02","text":"休闲时光","checked":false,"children":[{"id":"1bf52435e4f0af478dc9137ca7719fbb","text":"XX01","checked":false,"children":[],"parentId":"a921c809276dadf00bd45dd527564f02"}],"parentId":"0"},{"id":"ee43cc1ceb57a3793c5c11d6d632fd22","text":"摩登时代","checked":false,"children":[{"id":"fb9a268c6061d962dbb5fc5f55c803f8","text":"MD01","checked":false,"children":[],"parentId":"ee43cc1ceb57a3793c5c11d6d632fd22"}],"parentId":"0"}]
C# 实现Tree,包含parentId和children的更多相关文章
- [Flex] 组件Tree系列 —— 运用LabelFunction hasChildren getChildren设置Tree包含节点个数
mxml: <?xml version="1.0" encoding="utf-8"?> <!--功能描述:运用LabelFunction h ...
- [CareerCup] 4.8 Contain Tree 包含树
4.8 You have two very large binary trees: Tl, with millions of nodes, and T2, with hundreds of nodes ...
- Extjs4中的常用组件:Grid、Tree和Form
至此我们已经学习了Data包和布局等API.下面我们来学习作为Extjs框架中我们用得最多的用来展现数据的Grid.Tree和Form吧! 目录: 5.1. Grid panel 5.1.1. Col ...
- 【EasyUI学习-2】Easyui Tree的异步加载
作者:ssslinppp 1. 摘要 2. tree的相关介绍 3. 异步加载tree数据,并实现tree的折叠展开 3.1 功能说明: 3.2 前台代码 3.3 后台代码 4. 其他 1 ...
- layui基础上的tree菜单动态渲染;
var layout=[ { title:'脚本对象名称', treeNodes:true, headerClass:'value_col', colClass:'value_col', style: ...
- vue+Element实现tree树形数据展示
组件: Element(地址:http://element.eleme.io/#/zh-CN/component/tree):Tree树形控件 <el-tree ref="expand ...
- 蒙特卡罗方法、蒙特卡洛树搜索(Monte Carlo Tree Search,MCTS)初探
1. 蒙特卡罗方法(Monte Carlo method) 0x1:从布丰投针实验说起 - 只要实验次数够多,我就能直到上帝的意图 18世纪,布丰提出以下问题:设我们有一个以平行且等距木纹铺成的地板( ...
- js实现树级递归,通过js生成tree树形菜单(递归算法)
方法封装: /** * 数据转换为树形(递归),示例:toTreeByRecursion(source, 'id', 'parentId', null, 'children') * @param {A ...
- 无法解析指定对象的 TargetProperty (UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)“的异常解决
最近在写动画的时候做一个倒计时的效果,就是数字从大到小的一个动画,但是当我设置要new PropertyPath("XXXXXXX")的时候却报了标题的异常,各种报错.百度了好久也 ...
随机推荐
- leetcode 19. Remove Nth Node From End of List(链表)
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- linux 进程学习笔记-进程信号sigal
信号(或软中断)是在软件层次上对中断的一个模拟,其运行在“用户空间”,一个进程对另外一个或几个进程通过发送信号来实现异步通信.当接收进程接收到信号后,其可以注册一下处理函数来说对这些信号进行处理(也可 ...
- CodeForces - 1000E :We Need More Bosses(无向图缩点+树的直径)
Your friend is developing a computer game. He has already decided how the game world should look lik ...
- MySQL当月负毛利订单明细_20161027
#当月每天负毛利订单明细 SELECT c.ID,a.city AS 城市,a.username AS 用户ID,a.订单日期,a.订单号,a.销售确认额,a.成本额,a.毛利1, CASE THEN ...
- ACM学习历程——UVA127 "Accordian" Patience(栈, 链表)
Description ``Accordian'' Patience You are to simulate the playing of games of ``Accordian'' patie ...
- CentOS虚拟机通过主机WIFI上网
0 环境简介 环境如下: (1)宿主机为WIN7系统,连接内网,同时通过本机的WIFI上外网: (2)虚拟机为VMWare12下的CentOS7系统. 1 虚拟机设置 选择NAT 模式: 2 宿主机W ...
- tomcat 开机自启方法
1.把下面的代码保存为tomcat文件,并让它成为可执行文件 chmod 755 tomcat. #!/bin/bash # # /etc/rc.d/init.d/tomcat # init scri ...
- 2015年第六届蓝桥杯国赛试题(JavaA组)
1.结果填空 (满分15分)2.结果填空 (满分35分)3.代码填空 (满分31分)4.程序设计(满分41分)5.程序设计(满分75分)6.程序设计(满分103分) 1.标题:胡同门牌号 小明家住在一 ...
- Linux 之问题集锦(一)
1. 打开目录时,怎么只显示一个窗口 计算机 -- 编辑 -- 首选项 -- 行为 -- 总是总浏览器窗口中打开 2. linux中添加PATH时出现 Found a swap file by the ...
- Search in a Binary Search Tree
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...