C # implements tree, containing parentid and children

Source: Internet
Author: User

1. Define a type First

   Public classNode {[Jsonproperty (PropertyName="ID", nullvaluehandling =Nullvaluehandling.ignore)] Public stringID {Get;Set; } [Jsonproperty (PropertyName="text", nullvaluehandling =Nullvaluehandling.ignore)] Public stringText {Get;Set; } [Jsonproperty (PropertyName="checked", nullvaluehandling =Nullvaluehandling.ignore)] Public BOOLChecked {Get;Set; } [Jsonproperty (PropertyName="Children", nullvaluehandling =Nullvaluehandling.ignore)] PublicIlist<node> Children {Get;Set; } [Jsonproperty (PropertyName="ParentID", nullvaluehandling =Nullvaluehandling.ignore)] Public stringParentID {Get;Set; } }

2. Data sources, we find out from the database is generally the following data

Ilist<node> treelist =NewList<node>();
Treelist.add (NewNode {Id ="f31a347e4be70da6d925bfaddf0e896b", Text ="Business Classics", ParentID ="0"}); Treelist.add (NewNode {Id ="b50d381e694c0227242ff7b55685178c", Text ="LZ01", ParentID ="f31a347e4be70da6d925bfaddf0e896b"}); Treelist.add (NewNode {Id ="a921c809276dadf00bd45dd527564f02", Text ="Leisure Time", ParentID ="0"}); Treelist.add (NewNode {Id ="1BF52435E4F0AF478DC9137CA7719FBB", Text ="XX01", ParentID ="a921c809276dadf00bd45dd527564f02"}); Treelist.add (NewNode {Id ="Ee43cc1ceb57a3793c5c11d6d632fd22", Text ="Modern Times", ParentID ="0"}); Treelist.add (NewNode {Id ="Fb9a268c6061d962dbb5fc5f55c803f8", Text ="MD01", ParentID ="Ee43cc1ceb57a3793c5c11d6d632fd22"});

3. Recursive initialization tree

 // <summary>        ///Recursive initialization tree/// </summary>        /// <param name= "Nodes" >Results</param>        /// <param name= "ParentID" >Parent ID</param>        /// <param name= "Sources" >Data Source</param>        Private voidInittree (ilist<node> nodes,stringParentID, ilist<node>sources)            {Node Tempnode; //recursive search for child nodes            varTemptree = sources. Where (item = Item. ParentID = =parentid).            ToList (); foreach(Node rowinchTemptree) {Tempnode=NewNode () {Id=row. Id, Text=row. Text, ParentID=row. ParentID, children=NewList<node>()                }; Nodes.                ADD (Tempnode); Inittree (Tempnode.children, row.            Id, sources); }        }

4. Call to get results

 varTree =NewList<node>(); Inittree (Tree,"0", treelist);stringJSON =jsonconvert.serializeobject (tree);

//results obtained: [{"id": "f31a347e4be70da6d925bfaddf0e896b", "text": "Business classic", "checked": false, "children": [{"id": " B50d381e694c0227242ff7b55685178c "," text ":" LZ01 "," checked ": false," children ": []," ParentID ":" f31a347e4be70da6d925bfaddf0e896b "}]," ParentID ":" 0 "},{" id ":" a921c809276dadf00bd45dd527564f02 "," text ":" Leisure Time "," Checked ": false," children ": [{" id ":" 1BF52435E4F0AF478DC9137CA7719FBB "," text ":" XX01 "," checked ": false," children ": [] , "ParentID": "A921c809276dadf00bd45dd527564f02"}], "ParentID": "0"},{"id": "ee43cc1ceb57a3793c5c11d6d632fd22", " Text ":" Modern Times "," checked ": false," children ": [{" id ":" fb9a268c6061d962dbb5fc5f55c803f8 "," text ":" MD01 "," checked ": False, "children": [], "ParentID": "Ee43cc1ceb57a3793c5c11d6d632fd22"}], "ParentID": "0"}]

C # implements tree, containing parentid and children

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.