C# 递归范畴与内连接

C# 递归范畴与内连接,c#,asp.net,recursion,inner-join,C#,Asp.net,Recursion,Inner Join,我想在我的网页上为产品类别创建递归类别菜单。每个类别必须根据“产品”表上的CategoryId检索相关的第一项,但如果类别没有任何产品,则该类别应该消失。实际上,我可以很容易地使用非递归类别菜单的内部连接。我如何解决这个问题?有什么想法吗 我可以使用下面的方法,但是这个方法既业余又可能是空的 类别表 +--------------+---------------+------------+ | CategoryId | CategoryName | ParentId | +------

我想在我的网页上为产品类别创建递归类别菜单。每个类别必须根据“产品”表上的CategoryId检索相关的第一项,但如果类别没有任何产品,则该类别应该消失。实际上,我可以很容易地使用非递归类别菜单的内部连接。我如何解决这个问题?有什么想法吗

我可以使用下面的方法,但是这个方法既业余又可能是空的

类别表

+--------------+---------------+------------+
|  CategoryId  | CategoryName  | ParentId   |
+--------------+---------------+------------+
|      1       | Cookware      |   NULL     |
+--------------+---------------+------------+
|      2       | Tableware     |   NULL     |
+--------------+---------------+------------+
|      3       | Teapots       |     1      |
+--------------+---------------+------------+
|      4       | Cutleries     |     3      |
+--------------+---------------+------------+
|      5       | 30pcs Cutlery |     2      |
+--------------+---------------+------------+
+--------------+--------------+--------------------+------------+
|  ProductId   | ProductCode  | ProductName        | CategoryId |
+--------------+--------------+--------------------+------------+
|       1      |   G110090    |   Teapot           |      3     |
+--------------+--------------+--------------------+------------+
|       2      |   D220623    |   Cutlery Set      |      5     |
+--------------+--------------+--------------------+------------+ 
产品表

+--------------+---------------+------------+
|  CategoryId  | CategoryName  | ParentId   |
+--------------+---------------+------------+
|      1       | Cookware      |   NULL     |
+--------------+---------------+------------+
|      2       | Tableware     |   NULL     |
+--------------+---------------+------------+
|      3       | Teapots       |     1      |
+--------------+---------------+------------+
|      4       | Cutleries     |     3      |
+--------------+---------------+------------+
|      5       | 30pcs Cutlery |     2      |
+--------------+---------------+------------+
+--------------+--------------+--------------------+------------+
|  ProductId   | ProductCode  | ProductName        | CategoryId |
+--------------+--------------+--------------------+------------+
|       1      |   G110090    |   Teapot           |      3     |
+--------------+--------------+--------------------+------------+
|       2      |   D220623    |   Cutlery Set      |      5     |
+--------------+--------------+--------------------+------------+ 
递归分类方法

public string RecursiveCategory(IEnumerable<Category> category, int? parent)
{
    string catNode = string.Empty;
    if(category.Any(n=>n.ParentId == parent))
    {
        catNode += "<ul>";
            foreach(Category c in category)
            {
                catNode += "<li>";
                catNode += "<a href='/Detail/" + GetFirstItem(c.CategoryId).ProductId + "'>"+c.CategoryName+"</a>";
                catNode += RecursiveCategory(category, c.ParentId);
                catNode += "</li>";
            }
        catNode += "</ul>"
    }
    return catNode;
}
public Product GetFirstItem(int categoryId)
{
    Product prod = new Product();
    foreach(Product p in db.Product.Where(n=>n.CategoryId == categoryId))
    {
        prod.ProductId = p.ProductId;
        prod.ProductName = p.ProductName;
        ...
    }
    return prod;
}

尝试从给定的点构造层次结构(在第一次调用中使用null将给出包含每个类别的产品的完整树)。作为修改,如果需要,您可以使产品延迟加载:

public class Category
{
  IEnumerable<Category> Children {get;set;}
  IEnumerable<Product> Products {get;set;}
}

public IEnumerable<Category> GetCategory(int? parent)
{
  var result = new List<Category>();
  foreach (var cat in categories.Where(p => p.parentId = parent)
  {
     var generatedCategory = new Category();
     generatedCategory.Children = GetCategory(cat.id);
     generatedCategory.Products = products.Where(p => p.CategoryId = cat.CategoryId);
     result.Add(generatedCategory);
  }
  return result;
}
公共类类别
{
IEnumerable子项{get;set;}
IEnumerable乘积{get;set;}
}
公共IEnumerable GetCategory(int?父级)
{
var result=新列表();
foreach(类别中的变量cat.Where(p=>p.parentId=parent)
{
var generatedCategory=新类别();
generatedCategory.Children=GetCategory(cat.id);
generatedCategory.Products=Products.Where(p=>p.CategoryId=cat.CategoryId);
结果.添加(generatedCategory);
}
返回结果;
}

注意:我没有测试代码,只是一个如何轻松构建代码的指南。

您说“类别必须根据产品上的CategoryId检索相关的第一项”和“如果类别没有任何产品,则该类别应消失”。如果没有任何产品,它怎么能撤销第一项?@voo,我想我的问题有些错误,因为主要类别可能为空。我将尝试Jorge Córdoba编写的解决方案。