Basically, the Model is like this:
----------------------------------
| CAT_ID | CAT_NAME | CAT_PARENT |
----------------------------------
which make this a recursive relationship. But how should we use this model in our Asp.net mvc web application with least amount of query. My temporary solution is pass the object in to the function and let the function pass the object, instead of requerry, but this is the code i tried:
public string CategoryTree(this HtmlHelper html, IEnumerable<Category> categories, int? parent)
{
parent = parent ?? 0;
string htmlOutput = string.Empty;
var cat = categories.Where(d => d.CAT_PARRENT == parent.Value);
if (cat.Count() > 0)
{
htmlOutput += "<ul>";
foreach (Category category in cat)
{
htmlOutput += "<li>";
htmlOutput += category.CAT_NAME;
htmlOutput += html.CategoryTree(categories, category.CAT_ID);
htmlOutput += "</li>";
}
htmlOutput += "</ul>";
}
return htmlOutput;
}
but this generate like 4 query for 4 row of category. So this is not a good solution for this problem. Im using linq to sql.