[asp.net-mvc]Linq to SQL 中︰ 得到 5 顶级产品

标签: Asp.net-MVC Linq C#
发布时间: 2017/3/25 10:43:16
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我怎样才能顶 5 额定的产品通过 Linq to Sql 中?

enter image description here

我产品类是

public class Product
{
   [Key]
   [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
   public int ProductID { get; set; }
   public string Name { get; set; }
   public decimal Price { get; set; }
}

和我的 ProductReviews 类是

public class ProductReview
    {

            [Key]
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
            public int ProductReviewID { get; set; }
            public int ProductID { get; set; }
            public int Rating { get; set; }

            public virtual Product Product { get; set; }

    }

解决方法 1:

假设你想要一个简单平均的评级从每个产品的所有评论,下面的代码 (包括示例数据) 应该做的伎俩 (我测试过这对 LINQ 打击的对象,但它应该工作用 LINQ to SQL 也)︰

var products = new List<Product>
{
    new Product { ProductID = 1, Name= "Product 1", Price = 1.00m },
    new Product { ProductID = 2, Name= "Product 2", Price = 1.00m },
    new Product { ProductID = 3, Name= "Product 3", Price = 1.00m },
    new Product { ProductID = 4, Name= "Product 4", Price = 1.00m },
    new Product { ProductID = 5, Name= "Product 5", Price = 1.00m },
    new Product { ProductID = 6, Name= "Product 6", Price = 1.00m }
};

var productReviews = new List<ProductReview>
{
    new ProductReview { ProductReviewID = 1, ProductID = 1, Rating = 5 },
    new ProductReview { ProductReviewID = 2, ProductID = 1, Rating = 3 },
    new ProductReview { ProductReviewID = 3, ProductID = 2, Rating = 1 },
    new ProductReview { ProductReviewID = 4, ProductID = 3, Rating = 4 },
    new ProductReview { ProductReviewID = 5, ProductID = 4, Rating = 2 },
    new ProductReview { ProductReviewID = 6, ProductID = 5, Rating = 5 },
    new ProductReview { ProductReviewID = 7, ProductID = 6, Rating = 4 },
    new ProductReview { ProductReviewID = 8, ProductID = 6, Rating = 3 }
};

var averageProductRatings = from review in productReviews
                            group review by review.ProductID into product
                            select new
                            {
                                ProductId = product.Key,
                                AverageRating = product.Average(p => p.Rating)
                            };

var top5 = averageProductRatings.OrderByDescending(average => average.AverageRating).Take(5);

第一条语句正在审查数据,分组它由 ProductID 和计算的平均值 Rating 为每个产品。

第二个语句是以平均每个产品,然后给你与平均收视率最高的 5 个产品。

如果你想要做不同的事情 (像给更高的权重,对更多最近的评论) 你可以进入到一个自定义的函数,例如出该产品的评级工作的"产品":

var averageProductRatings = from review in productReviews
                            group review by review.ProductID into product
                            select new
                            {
                                ProductId = product.Key,
                                AverageRating = GetProductRatingFromReviews(product)
                            };

private  double GetProductRatingFromReviews(IGrouping<int, ProductReview> productReviews)
{
    // Work out the aggregate rating to give the product here and return it

    foreach (var item in productReviews)
    {

    }
    return -1;
}
官方微信
官方QQ群
31647020