如何显示特定类别的前5个产品

时间:2014-12-25 09:29:01

标签: sql-server linq entity-framework select nopcommerce

我想在特定类别下获取前5个最受欢迎的产品计算机

这是我的班级档案:

  public partial class Category {
       public int Id { get; set; }
       public string Name { get; set; }
       public int ParentCategoryId { get; set; } //reference to Id 
       public ICollection<Category> _subcategories;
  } 

  public partial class ProductCategory {
       public int Id { get; set; }
       public int ProductId { get; set; }
       public int CategoryId { get; set; }
       public virtual Category Category { get; set; }
       public virtual Product Product { get; set; }
  }

 public partial class Product {
       public int Id { get; set; }
       public string Name { get; set; }                     
       public int ProductViewcount { get; set; }//indicated how many times product has been viewed means most popular product.
 }

这里是一个包含记录的示例小提琴:http://www.sqlfiddle.com/#!3/20cba

最终输出:

ProductId ProductName

1 hp

2 compaq

3 lenovo

此处问题计算机是我的主要类别,而笔记本电脑是计算机的子类别,所以当我说获得前5名计算机产品我想检索儿童类别记录也像小提琴一样我想得到所有儿童类别的记录笔记本电脑

我知道我必须在 ProductViewCount 获取前5个产品上执行订单。

1 个答案:

答案 0 :(得分:1)

首先,让我们来定义我们需要的东西。我们需要一个包含以下字段的表格(视图):ProductIdProductNameProductViewCountRootCategoryId

想象一下,Category表已经有RootCategoryId字段。然后我们可以使用此查询来接收结果:

SELECT P.Id AS 'ProductId', P.Name AS 'ProductName', PVC.ProductViewCount, C.RootCategoryId
FROM Category C
  INNER JOIN ProductCategory PC ON PC.CategoryId = C.Id
  INNER JOIN Product P ON PC.ProductId = P.Id
  INNER JOIN ProductViewCount PVC ON P.Id = PVC.ProductId

不幸的是,Category表没有必要的字段。因此,我们需要一个包含字段CategoryCategoryId的表格(而不是RootCategoryId)。

对于顶级类别CategoryIdRootCategoryId是相同的:

SELECT Id AS 'CategoryId', Id AS 'RootCategoryId'
FROM Category
WHERE ParentCategoryId = 0

对于后代类别CategoryIdIdRootCategoryId与父级相同。所以我们可以写CTE:

WITH RecursiveCategory(CategoryId, RootCategoryId)
AS
(
    SELECT Id AS 'CategoryId', Id AS 'RootCategoryId'
    FROM Category
    WHERE ParentCategoryId = 0
    UNION ALL
    SELECT C.Id AS 'CategoryId', RC.RootCategoryId
    FROM Category C
        INNER JOIN RecursiveCategory RC ON C.ParentCategoryId = RC.CategoryId
)
. . .

现在让我们把各个部分拼凑起来。我们需要一个视图:

CREATE VIEW ProductWithRootCategory
AS
WITH RecursiveCategory(CategoryId, RootCategoryId)
AS
(
    SELECT Id AS 'CategoryId', Id AS 'RootCategoryId'
    FROM Category
    WHERE ParentCategoryId = 0
    UNION ALL
    SELECT C.Id AS 'CategoryId', RC.RootCategoryId
    FROM Category C
        INNER JOIN RecursiveCategory RC ON C.ParentCategoryId = RC.CategoryId
)
SELECT P.Id AS 'ProductId', P.Name AS 'ProductName', PVC.ProductViewCount, RC.RootCategoryId
FROM RecursiveCategory RC
  INNER JOIN ProductCategory PC ON PC.CategoryId = RC.CategoryId
  INNER JOIN Product P ON PC.ProductId = P.Id
  INNER JOIN ProductViewCount PVC ON P.Id = PVC.ProductId

现在您可以将视图添加到EF并使用:

int rootCategoryId = 1; // Is's Computers
var productsInRootCategory = ProductWithRootCategory.Where(pwrc => pwrc.RootCategoryId == rootCategoryId);
var top5Products = productsInRootCategory.OrderBy(pwrc => pwrc.ProductViewCount).Take(5);