Linq to SQL:获得前5名评级产品

时间:2015-10-09 17:18:36

标签: c# asp.net-mvc linq

如何通过Linq到Sql获得前5名评级产品?

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

    }

3 个答案:

答案 0 :(得分:2)

假设您想对每个产品的所有评论采用简单的评级平均值,下面的代码(包括示例数据)应该可以解决问题(我已针对对象测试了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;
}

答案 1 :(得分:1)

我希望我能正确理解你的用例。产品和产品评论与您的​​相关产品系列相似:

productReviews.GroupBy(q => q.ProductID)
            .Select(t => new {ID = t.Key, ReviewAvg = t.Average(q => q.Rating})
            .OrderByDescending(q => q.ReviewAvg).Take(5).Select(t => products.FirstOrDefault(q => q.ProductID == t.ID));

我首先按相关产品ID对所有评论进行分组(如果产品本身进行分组会更好),然后针对每种产品计算平均评分,并选择前5个产品。

答案 2 :(得分:0)

您需要指定检测热门商品的条件。然后你可以做这样的事情

var top5Products = database.Products.Where(product => _your condition_).OrderBy(product => product.SomeField).Take(5).ToList();