Thank you foryour reply.turned it intoone to one.Well,I have a questionlike this?Is theperformanceof thismethod?because,all
occurrences of
the function,the databasebecomesquestioning.How
towritethe
performance
of this
function?
Thank you.
The approach you are using at the moment makes a call to the database on every loop. That may affect performance badly. You can change it so that the first call to the database gets ALL the data, and that is passed in to the recursive function instead:
@helper Fncat(IEnumerable<dynamic> data, int cat, int step, int hcat) {
var results = data.Where(item => item.ParentId == cat)
.Select(row => new {
Id = row.Id,
Category = row.Category,
ParentId = row.ParentId
});
step++;
foreach(var row in results) {
var spc = "";
for(int i = 0; i < step * 8; i++) {
spc += " ";
}
var ssec = (row.Id == hcat) ? " selected" : "";
<option value="@row.Id" @ssec>@Html.Raw(spc)@row.Category</option>
@Fncat(data, (int)row.Id, step, hcat)
}
step--;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
@{
var db = Database.Open("SqlTests");
var data = db.Query("Select * From cat");
<select>
@Fncat(data, 0, 0, Request["cat"].AsInt(-1))
</select>
}
</body>
</html>
pranavkm
Participant
796 Points
106 Posts
Re: Nested Category Recurse
Jul 06, 2011 06:25 PM|LINK
Shouldn't you be able to translate your code verbatim?
@helper Fncat(Database db, int cat, int step, int hcat) { var results = db.Query("Select * From cat Where ParentId= @0", cat); step++; foreach(var row in results) { var spc = ""; for(int i = 0; i < step * 8; i++) { spc += " "; } var ssec = (row.Id == hcat) ? " selected" : ""; <option value="@row.Id" @ssec>@Html.Raw(spc)@row.Category</option> @Fncat(db, (int)row.Id, step, hcat) } step--; } @{ var db = Database.Open("Test"); <select name="cat"> @Fncat(db, 1, 0, Request["cat"].AsInt(-1)) </select> }You may also want to look into using optgroups instead of spaces to represent hierarchies in a select list.
aspnet1071
Member
64 Points
35 Posts
Re: Nested Category Recurse
Jul 06, 2011 08:08 PM|LINK
Thank you for your reply. turned it into one to one. Well, I have a question like this? Is the performance of this method? because, all occurrences of the function, the database becomes questioning. How to write the performance of this function? Thank you.
Mikesdotnett...
All-Star
154858 Points
19858 Posts
Moderator
MVP
Re: Nested Category Recurse
Jul 06, 2011 08:49 PM|LINK
The approach you are using at the moment makes a call to the database on every loop. That may affect performance badly. You can change it so that the first call to the database gets ALL the data, and that is passed in to the recursive function instead:
@helper Fncat(IEnumerable<dynamic> data, int cat, int step, int hcat) { var results = data.Where(item => item.ParentId == cat) .Select(row => new { Id = row.Id, Category = row.Category, ParentId = row.ParentId }); step++; foreach(var row in results) { var spc = ""; for(int i = 0; i < step * 8; i++) { spc += " "; } var ssec = (row.Id == hcat) ? " selected" : ""; <option value="@row.Id" @ssec>@Html.Raw(spc)@row.Category</option> @Fncat(data, (int)row.Id, step, hcat) } step--; } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> </head> <body> @{ var db = Database.Open("SqlTests"); var data = db.Query("Select * From cat"); <select> @Fncat(data, 0, 0, Request["cat"].AsInt(-1)) </select> } </body> </html>Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
aspnet1071
Member
64 Points
35 Posts
Re: Nested Category Recurse
Jul 07, 2011 07:58 AM|LINK
thank you.
var results = data.Where(item => item.ParentId == cat)
.Select(row => new {
Id = row.Id,
Category = row.Category,
ParentId = row.ParentId
Order By Sr Asc, Cat Desc This code how include?
Mikesdotnett...
All-Star
154858 Points
19858 Posts
Moderator
MVP
Re: Nested Category Recurse
Jul 07, 2011 08:54 AM|LINK
You can use the Linq OrderBy, OrderByDescending, ThenBy, ThenByDescending methods:
For example:
var results = data.Where(item => item.ParentId == cat) .Select(row => new { Id = row.Id, Category = row.Category, ParentId = row.ParentId }).OrderBy(item => item.Category);Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
aspnet1071
Member
64 Points
35 Posts
Re: Nested Category Recurse
Jul 07, 2011 09:06 AM|LINK
I should write to the two columns at the same time?
Order By Column1 ASC, Column2 DESC
Mikesdotnett...
All-Star
154858 Points
19858 Posts
Moderator
MVP
Re: Nested Category Recurse
Jul 07, 2011 12:06 PM|LINK
Just chain the method calls:
var results = data.Where(item => item.ParentId == cat) .OrderBy(item => item.Column1).ThenByDescending(item => item.Column2) .Select(row => new { Id = row.Id, Category = row.Category, ParentId = row.ParentId });Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
aspnet1071
Member
64 Points
35 Posts
Re: Nested Category Recurse
Jul 07, 2011 02:10 PM|LINK
Thank you very much