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 = "0" }); treeList.Add(new Node { Id = "b50d381e694c0227242ff7b55685178c", Text = "LZ01", ParentId = "f31a347e4be70da6d925bfaddf0e896b" }); treeList.Add(new Node { Id = "a921c809276dadf00bd45dd527564f02", Text = "休闲时光", ParentId = "0" }); treeList.Add(new Node { Id = "1bf52435e4f0af478dc9137ca7719fbb", Text = "XX01", ParentId = "a921c809276dadf00bd45dd527564f02" }); treeList.Add(new Node { Id = "ee43cc1ceb57a3793c5c11d6d632fd22", Text = "摩登时代", ParentId = "0" }); 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, "0", 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"}]

 

你可能感兴趣的:(children)