I want to display categories and subcategories like so:
Category 1
Subcategory 1
Subcategory 2
Subcategory 3
Category 2
Subcategory 5
Subcategory 6
Subcategory 7
In other words, foreach category, display the subcategories that belong to each one underneath.
My two tables are like so:
Category-
CategoryID
Name
SubCategory-
SubCategoryID
SubCategoryName
CategoryID
I have a foreign key from category to subcategory a one to many.
Here is where I got in the code, which display all sub categories foreach category.
public void displayLinqCategory()
{
MyDataContext dbm = new MyDataContext();
var q = from category in dbm.Categories
join subCat in dbm.SubCategories
on category.CategoryID equals subCat.CategoryID
select new { category.Name, subCat.SubCategoryName };
resultSpan.InnerHtml += "<table>";
foreach (var c in q)
{
resultSpan.InnerHtml += "<tr><td>" + c.Name + "</td></tr>";
foreach (var s in q)
{
resultSpan.InnerHtml += "<tr><td> " + s.SubCategoryName + "</td></td>";
}
}
resultSpan.InnerHtml += "</table>";
}