Home > Net >  ASP.NET how to convert Lambd type query results into JSON with tree structure (children)
ASP.NET how to convert Lambd type query results into JSON with tree structure (children)

Time:09-21

, the purpose is to transform the query JSON conversion after the spanning tree menu structure.
The database menu structure is as follows:
AuthID AuthName ParentID type AuthUrl Icon
1 0 menu System management/System/UserInfoList & amp; # xe716;
2 user management menu/System/1 UserInfoList & amp; # xe770;
3 menu management 1 menu/System/GetMenuList & amp; # xe66b;
4 0 menu/procurement management System/GetMenuList & amp; # xe657;
5 purchasing application 4 menu/System/GetMenuList & amp; # xe657;

Get the menu access list # region information
Public ActionResult GetMenu ()
{
//query all the menu data
Var AuthorizeInfo=the authorize. Select (u=& gt; True);
Var tempdata=https://bbs.csdn.net/topics/from a AuthorizeInfo in
Select new
{
Authid=a.A uthID,
ParentID=Amy polumbo arentID,
Title=a.A uthName,
Href=https://bbs.csdn.net/topics/a.AuthUrl,
Icon=Anderson, con,
Spread=false,
The children=Amy polumbo arentID

};

Return a Json (tempdata, JsonRequestBehavior. AllowGet);
}
# endregion
After the search, get the JSON format for:
[{"Authid":1,"ParentID":0,"title":"系统管理","href":"/System/UserInfoList","icon":"\u0026#xe716;","spread":false,"children":0},{"Authid":2,"ParentID":1,"title":"用户管理","href":"/System/UserInfoList","icon":"\u0026#xe770;","spread":false,"children":1},{"Authid":3,"ParentID":1,"title":"菜单管理","href":"/System/GetMenuList","icon":"\u0026#xe66b;","spread":false,"children":1},{"Authid":4,"ParentID":0,"title":"采购管理","href":"/System/GetMenuList","icon":"\u0026#xe657;","spread":false,"children":0},{"Authid":5,"ParentID":4,"title":"采购申请","href":"/System/GetMenuList","icon":"\u0026#xe657;","spread":false,"children":4}]

I want to json format for:

{
"Title" : "system management",
"Icon" : "& amp; # xe716;" ,
"Href" : ", "
"Spread" : false,
"Children" : [
{
"Title" : "user management",
"Icon" : "& amp; # xe770;" ,
"Href" : "/System/UserInfoList",
"Spread" : false
},
{
"Title", "management" menu,
"Icon" : "& amp; # xe770;" ,
"Href" : "/System/GetMenuList",
"Spread" : false
}
]

}
]
Here to bring out the children of the following relations node
In asking you a great god help, thank you

CodePudding user response:

In general is first you take out a list of data, and then get a tree structure through recursion

CodePudding user response:

Thank ourhouzi on the second floor, has found a solution, I changed the query conditions, can be a tree structure shows the
Get the menu access list # region information

Public ActionResult GetMenu ()
{
//query all the menu data
Var AuthorizeInfo=the authorize. Select (u=& gt; True);
Var tempdata=https://bbs.csdn.net/topics/from a AuthorizeInfo in
Where Amy polumbo arentID==0
Select new
{
Authid=a.A uthID,
ParentID=Amy polumbo arentID,
Title=a.A uthName,
Href=https://bbs.csdn.net/topics/a.AuthUrl,
Icon=Anderson, con,
Spread=false,
Children=the from t in AuthorizeInfo
Where t.P arentID==a.A uthID
Select new
{
Title=t.A uthName,
Href=https://bbs.csdn.net/topics/t.AuthUrl,
Icon=t.I con,
Spread=false,
}
};

Return a Json (tempdata, JsonRequestBehavior. AllowGet);

}
# endregion
This one condition, show only the level of the root directory,
0 indicates the root directoryWhere Amy polumbo arentID==0
This sentence said: root directory
all the child nodes of the queryChildren=the from t in AuthorizeInfo
Where t.P arentID==a.A uthID
Select new
{
Title=t.A uthName,
Href=https://bbs.csdn.net/topics/t.AuthUrl,
Icon=t.I con,
Spread=false,
}

As shown in the figure below:
  • Related