Recursive implementation of unlimited menu, product classification, building-style comments, messages and other functions.
The following code cannot be used directly
CREATE TABLE [dbo]. [P_category] ([Code] [varchar] (*) NOT NULL PRIMARY key,[parent_code] [varchar] (null,[depth] [int] null,[name] [varchar] () NULL) Goinsert [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 101 ', n ' 1 ', 2, n ' wooden doors ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 10101 ', n ' 101 ', 3, n ' Indoor wooden Doors ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 1010101 ', n ' 10101 ', 4, n ' A02 ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 101010101 ', n ' 10101 ', 5, n ' A01 ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 10101010101 ', n ' 101010101 ', 6, n ' A0101 ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 102 ', n ' 1 ', 2, n ' B ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 10201 ', n ' 102 ', 3, n ' B1 ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 1020101 ', n ' 10201 ', 4, n ' B2 ')
Private list<category> categorycachealllist {get; set;} [Route ("")]public httpresponsemessage Get () {var list = Cachehelper<list<category>>. GetCache (Categoryalllistcachekey), if (list = = null) {categorycachealllist = Categoryservice.getcachelist (); Get all data inside the database List = new list<category> (); Categoryjson (List, "1"); Cachehelper<list<category>>. Setcache (Categoryalllistcachekey, list);} Return Request.createresponse (Httpstatuscode.ok, list); The following code does not try//string json = jsonconvert.serializeobject (categorylist, formatting.indented);//return json;} <summary>///Get Brother Node///</summary>///<param name= "CategoryList" ></param>///<param Name= "Parentcode" ></param>public void Categoryjson (list<category> categorylist, string parentCode) { var list = Categorycachealllist.findall (p = = P.parentcode = = Parentcode), if (list. Count > 0) {foreach (var item in list) {Categorytreejson (item, item. Code); Categorylist.add (item);}}} <summary>///Recursive sub-object///</summary>///<param name= "sbcategory" ></param>///<param name= " Parentcode "></param>private void Categorytreejson (Category sbcategory, string parentcode) {var list = Categorycachealllist.findall (p = = P.parentcode = = Parentcode), if (list. Count > 0) {sbcategory.children = new list<category> (); foreach (var item in List) {Categorytreejson (item, item. Code); SbCategory.Children.Add (item);}}}
Recursive entity recursive JSON format for tree structure