I want to show categories and subcategories, for example:
Category 1
Subcategory 1
Subcategory 2
Subcategory 3
Category 2
Subcategory 5
Subcategory 6
Subcategory 7
In other words, the foreach category displays subcategories that belong to each of them below.
My two tables look like this:
Category-
CategoryID
Name
Subcategory -
SubCategoryID
SubCategoryName
CategoryID
I have a foreign key from category to subcategory from one to many.
This is where I got into the code that displays all categories of foreach subcategories.
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>";
}