Sharing a recursive infinite-level splicing JSON method---ExtJS treepanel and Treegrid are all applicable (ef,lambda,linq,iqueryable,list)

Source: Internet
Author: User

Not much to say, first on the entity class, if you are not codefirst, it as a data table structure.

Here's how the underlying basedal gets the data (if you don't have a base class, write directly on your DAL layer and the BLL layer)

Here's the Baseservice method.

The following methods are used to stitch strings

Principal Method--

1 /// <summary>2         ///get the JSON of tree Treepanel3         /// </summary>4         /// <returns></returns>5          Public stringGetallleftmenu4treepaneljson ()6         {7StringBuilder Allleftmenujsonstr =NewStringBuilder ();8Allleftmenujsonstr.append ("[");9             intnormal = Convert.ToInt32 (Common.Enum.DelFlagEnum.Normal);//You cannot have type conversions in a lambda expressionTenlist<adminleftmenuinfo> Adminleftmenuinfos = getentitesqueryable (m = M.delflag = =normal). ToList (); One             foreach(varAdminleftmenuinfoinchAdminleftmenuinfos) A             { -                 if(Adminleftmenuinfo.parentid = =0)//if the parent ID of the element is 0, it is the top-level element -                 { theAllleftmenujsonstr.append ("{"); -Allleftmenujsonstr.append ("\ "text\": \ ""+ Adminleftmenuinfo.pagename +" \"  "); -Allleftmenujsonstr.append (","); -  +Allleftmenujsonstr.append ("\ "checked\": false"); -Allleftmenujsonstr.append (","); +Allleftmenujsonstr.append ("\ "Cls\": \ "Folder\""); A  at  -list<adminleftmenuinfo> childrens = adminleftmenuinfos.where (m = M.parentid = =adminleftmenuinfo.id). ToList (); -                     if(Childrens. Count! =0)//if he had a son---splicing his son . -                     { -Allleftmenujsonstr.append (","); -Allleftmenujsonstr.append ("\ "children\":"); in                         //var newchildrens= - doappendchildjson4treepanel (childrens, allleftmenujsonstr); to  +                     } -  theAllleftmenujsonstr.append ("},"); *  $                 }Panax Notoginseng             } -Allleftmenujsonstr.remove (Allleftmenujsonstr.length-1,1); theAllleftmenujsonstr.append ("]"); +             returnallleftmenujsonstr.tostring (); A         } the         /// <summary> +         ///method of recursive sub-leaves for all treepanel -         /// </summary> $         /// <param name= "childrens" >Son as parameter</param> $         /// <param name= "Allleftmenujsonstr" >Pass in the JSON string that needs to be stitched up.</param> -         Private voidDoappendchildjson4treepanel (list<adminleftmenuinfo>Childrens, StringBuilder allleftmenujsonstr) -         { theAllleftmenujsonstr.append ("["); -             foreach(varChildreninchchildrens)Wuyi             { the                 //judge whether children has a son, if there is a folder, if no son is the leaf, -                 //and Checked=false; Wu                 //var son = getentitesqueryable (m = M.parentid = = Children. ID); -list<adminleftmenuinfo> son = getentitesqueryable (m = M.parentid = =children. ID). ToList (); About                 if(son. Count = =0)//no son, so he's the leaf. $                 { -Allleftmenujsonstr.append ("{"); -Allleftmenujsonstr.append ("\ "text\": \ ""+ Children. PageName +"\""); -Allleftmenujsonstr.append (","); AAllleftmenujsonstr.append ("\ "leaf\": True"); +Allleftmenujsonstr.append (","); theAllleftmenujsonstr.append ("\ "checked\": false"); -Allleftmenujsonstr.append ("},"); $                 } the                 Else//with a son, then he's the folder . the                 { theAllleftmenujsonstr.append ("{"); theAllleftmenujsonstr.append ("\ "text\": \ ""+ Children. PageName +"\""); -Allleftmenujsonstr.append (","); inAllleftmenujsonstr.append ("\ "Cls\": \ "Folder\""); theAllleftmenujsonstr.append (","); theAllleftmenujsonstr.append ("\ "checked\": false"); AboutAllleftmenujsonstr.append (","); theAllleftmenujsonstr.append ("\ "children\":"); the Doappendchildjson4treepanel (son, Allleftmenujsonstr); If you have a son, you're going to have to pass. theAllleftmenujsonstr.append ("},"); +                 } -  the             }BayiAllleftmenujsonstr.remove (Allleftmenujsonstr.length-1,1); theAllleftmenujsonstr.append ("]"); the}

The reason why I want to turn IQueryable into a list is simple:

EF lazy Loading will execute the code at the time of the call, such as when we judge Iqueryable<t> T==null, and then execute the previously defined query action.

If IQueryable is operated here all the time, when we first execute a deferred load action query database, the second time you want to use the same IQueryable, you will get an error:

The error content is probably SqlDataReader has opened a link, please close this link first. What is the cause of the error is that after the first delay loading, the connection is not closed,

In the second call collection offline query, the code will think you have to re-execute the query, this is not allowed, so in order to offline operation, the conversion for the list collection, in memory random play.

Finally, Bong

Sharing a recursive infinite-level splicing JSON method---ExtJS treepanel and Treegrid are all applicable (ef,lambda,linq,iqueryable,list)

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.