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的更多相关文章

  1. [Flex] 组件Tree系列 —— 运用LabelFunction hasChildren getChildren设置Tree包含节点个数

    mxml: <?xml version="1.0" encoding="utf-8"?> <!--功能描述:运用LabelFunction h ...

  2. [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 ...

  3. Extjs4中的常用组件:Grid、Tree和Form

    至此我们已经学习了Data包和布局等API.下面我们来学习作为Extjs框架中我们用得最多的用来展现数据的Grid.Tree和Form吧! 目录: 5.1. Grid panel 5.1.1. Col ...

  4. 【EasyUI学习-2】Easyui Tree的异步加载

    作者:ssslinppp       1. 摘要 2. tree的相关介绍 3. 异步加载tree数据,并实现tree的折叠展开 3.1 功能说明: 3.2 前台代码 3.3 后台代码 4. 其他 1 ...

  5. layui基础上的tree菜单动态渲染;

    var layout=[ { title:'脚本对象名称', treeNodes:true, headerClass:'value_col', colClass:'value_col', style: ...

  6. vue+Element实现tree树形数据展示

    组件: Element(地址:http://element.eleme.io/#/zh-CN/component/tree):Tree树形控件 <el-tree ref="expand ...

  7. 蒙特卡罗方法、蒙特卡洛树搜索(Monte Carlo Tree Search,MCTS)初探

    1. 蒙特卡罗方法(Monte Carlo method) 0x1:从布丰投针实验说起 - 只要实验次数够多,我就能直到上帝的意图 18世纪,布丰提出以下问题:设我们有一个以平行且等距木纹铺成的地板( ...

  8. js实现树级递归,通过js生成tree树形菜单(递归算法)

    方法封装: /** * 数据转换为树形(递归),示例:toTreeByRecursion(source, 'id', 'parentId', null, 'children') * @param {A ...

  9. 无法解析指定对象的 TargetProperty (UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)“的异常解决

    最近在写动画的时候做一个倒计时的效果,就是数字从大到小的一个动画,但是当我设置要new PropertyPath("XXXXXXX")的时候却报了标题的异常,各种报错.百度了好久也 ...

随机推荐

  1. CodeForcesdiv1:995C - Leaving the Bar(随机算法+贪心)

    For a vector →v=(x,y)v→=(x,y), define |v|=√x2+y2|v|=x2+y2. Allen had a bit too much to drink at the ...

  2. 【Lintcode】159.Find Minimum in Rotated Sorted Array

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  3. H+ Se7en WebUI

    http://www.zi-han.net/theme/hplus/webim.html

  4. SQL 调用 webservice

    webservice 需要配置为get 方式 sp_configure 'show advanced options', 1; GO RECONFIGURE; GO sp_configure 'Ole ...

  5. Java 打包成exe安装包

    1.在eclipse中导出Runnable JAR file 2.选择主函数所在的类和输出位置后finish: 3.这里选择的打包工具是exe4j,在网上找序列号注册一下,否则在打完后在exe运行时, ...

  6. WCF IIS部署

    创建WCFHost应用程序 Iservice.cs using System; using System.Collections.Generic; using System.Linq; using S ...

  7. Spring入门第十六课

    接上一次讲课 先看代码: package logan.spring.study.annotation.repository; public interface UserRepository { voi ...

  8. HDU - 2962 Trucking SPFA+二分

    Trucking A certain local trucking company would like to transport some goods on a cargo truck from o ...

  9. asp.net清空页面上的所有TextBox

    Asp.net中一次性清空页面上的所有TextBox中的内容,由于TextBox在客户端以<input type=”text”>形式来呈现的,因此解决方案有客户端和服务器端两种方式,服务器 ...

  10. $(this).index()与$(obj).index(this)的区别

    <div> <b>this is b</b> </div> <div> <p>this is span</p> &l ...