再帰的なカテゴリを作成したい.結合を使用している間はアイテムをロードしない.カテゴリにアイテムがない場合は表示されないため、外部結合したくない.どうすればこの問題を解決できますか?
public string CategoryTree(int depth)
{
StringBuilder sBuilder;
using (DataContext db = new DataContext())
{
var query = from n in db.Categories join m in db.Products on n.CatId equals m.CategoryId where n.ParentId == depth select new{n, m};
sBuilder = new StringBuilder("<ul>");
foreach (var q in query)
{
sBuilder.Append("<li>");
sBuilder.Append("<a href='/Product/"+q.m.ProdId+">'"+q.n.Title+"</a>"+CategoryTree(q.n.CatId));
sBuilder.Append("</li>");
}
sBuilder.Append("</ul>");
}
return sBuilder.ToString();
}
Categories
+---------+---------------+------------+
| CatId | Title | ParentId |
+---------+---------------+------------+
| 1 | Electronic | NULL |
+---------+---------------+------------+
| 2 | Computer | NULL |
+---------+---------------+------------+
| 3 | Television | 1 |
+---------+---------------+------------+
| 4 | Led | 3 |
+---------+---------------+------------+
| 5 | Printer | 2 |
+---------+---------------+------------+
| 6 | Laser Printer | 5 |
+---------+---------------+------------+
Products
+---------+---------------+------------+
| ProdId | ProductName | CategoryId |
+---------+---------------+------------+
| 1 | Samsung LED | 4 |
+---------+---------------+------------+
| 2 | Sony LED TV | 4 |
+---------+---------------+------------+