C#実装Tree、parentIdとchildrenを含む

9983 ワード

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"}]