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")的时候却报了标题的异常,各种报错.百度了好久也 ...
随机推荐
- 0x01
随便记录点想法什么的, 这个博客的编辑界面挺简陋的...
- MAC OS Sierra 10.12.6 下对固态硬盘SSD 开启TRIM功能
这个是对于不是mac原装SSD的情况下才做的操作... 大家都知道,苹果店卖的SSD硬盘那怕就是一个256G的也要1000多人民币,而市场上的也就400-500左右人民币,整整少了一半还要多,可见JS ...
- bjwc Day1 暴力大战
今天终于有题了... 题目是COCI2016/2017 Round #4 T1一看就是NP问题,k<=50,开始想暴力,想了个n^4的,大概能过,就没去管它 T2想得太naive,丢了100分给 ...
- python 动态添加属性及方法及“__slots__的作用”
1.动态添加属性 class Person(object): def __init__(self, newName, newAge): self.name = newName self.age = n ...
- Oracle字符串截断
字段.SUBSTR(string,start_position,[length]) 求子字符串,返回字符串解释:string 元字符串, start_position 开始位置(从0开始), leng ...
- Oracle 11gr2的完全卸载
Oracle 11gr2的完全卸载方式与前些版本有了改变,运行D:\app\Administrator\product\11.2.0\dbhome_1\deinstall的deinstall.bat批 ...
- textarea的style="resize:none;"
<textarea class="form-control" id="gryj" rows="3" maxlength="3 ...
- Manacher's Algorithm 马拉车算法(最长回文串)
这个马拉车算法Manacher‘s Algorithm是用来查找一个字符串的最长回文子串的线性方法,由一个叫Manacher的人在1975年发明的,这个方法的最大贡献是在于将时间复杂度提升到了线性,这 ...
- SPOJ IAPCR2F 【并查集】
思路: 利用并查集/DFS都可以处理连通问题. PS:注意Find()查找值和pre[]值的区别. #include<bits/stdc++.h> using namespace std; ...
- opencv多版本安装
使用命令查看当前的opencv版本: pkg-config --modversion opencv 自带的是opencv-2.4.9 安装opencv3.1 安装opencv的依赖项 sudo apt ...