简体   繁体   English

递归类别和内部联接

[英]Recursive category and inner join

I want to make recursive category menu for product categories on my web page.Each category must retrieve related first item according to CategoryId on "Product" table but this category should be disappear if category hasn't any product.Actually, i can make to easily with using INNER JOIN for non-recursive category menu.How can i solve this issue?Is there any idea? 我想在我的网页上为产品类别创建递归类别菜单。每个类别都必须根据“产品”表上的CategoryId检索相关的第一项,但是如果类别没有任何产品,则此类别应该消失。实际上,我可以通过将INNER JOIN用于非递归类别菜单可以轻松解决该问题吗?

I can use a method as follow but this method both amateur and may be null first item. 我可以使用以下方法,但是该方法既业余,也可能为空。

Category table 分类表

+--------------+---------------+------------+
|  CategoryId  | CategoryName  | ParentId   |
+--------------+---------------+------------+
|      1       | Cookware      |   NULL     |
+--------------+---------------+------------+
|      2       | Tableware     |   NULL     |
+--------------+---------------+------------+
|      3       | Teapots       |     1      |
+--------------+---------------+------------+
|      4       | Cutleries     |     3      |
+--------------+---------------+------------+
|      5       | 30pcs Cutlery |     2      |
+--------------+---------------+------------+

Product table 产品表

+--------------+--------------+--------------------+------------+
|  ProductId   | ProductCode  | ProductName        | CategoryId |
+--------------+--------------+--------------------+------------+
|       1      |   G110090    |   Teapot           |      3     |
+--------------+--------------+--------------------+------------+
|       2      |   D220623    |   Cutlery Set      |      5     |
+--------------+--------------+--------------------+------------+ 

RecursiveCategory method RecursiveCategory方法

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;
}

GetFirstItem method GetFirstItem方法

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;
}

Try this to construct a hierarchy from a given point (using null in the first call will give you the full tree with the products for each category). 尝试此操作从给定的点构造层次结构(在第一个调用中使用null将为您提供包含每个类别的产品的完整树)。 As a modification you could make product lazy load if you need to: 作为修改,如果需要,可以使产品延迟加载:

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;
}

Note: I haven't tested the code just a guide about how to construct it easily. 注意:我还没有测试过代码,只是关于如何轻松构造代码的指南。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM