我该如何组合这些线?

时间:2015-10-09 18:30:50

标签: c# linq

我的Linq查询

List<TopRated> TopRated = (from review in db.ProductReviews
                            group review by review.ProductID into product
                            select new TopRated
                            {
                              ProductName = product.Select(p => p.Products.Name).FirstOrDefault(),
                              Price = product.Select(p => p.Products.Price).FirstOrDefault(),
                              PriceOffer = product.Select(p => p.Products.PriceOffer).FirstOrDefault(),
                              ProductId = product.Key,
                              AverageRating = product.Average(p => p.Rating)
                            }).ToList();

我可以将这些行组合成一个吗?

ProductName = product.Select(p => p.Products.Name).FirstOrDefault(),
Price = product.Select(p => p.Products.Price).FirstOrDefault(),
PriceOffer = product.Select(p => p.Products.PriceOffer).FirstOrDefault(),

我认为我会增加过载&#34;通过做上面这样的事情。

4 个答案:

答案 0 :(得分:3)

您可以使用使用范围变量的let子句。在我看来,它创建了一种直观而优雅的语法:

    var TopRated = (from review in db.ProductReviews
                    group review by review.ProductID into product
                    let firstProduct = product.FirstOrDefault()
                    select new 
                    {
                        ProductName = firstProduct.Products.Name,
                        Price = firstProduct.Products.Price,
                        PriceOffer = firstProduct.Products.PriceOffer,
                        ProductId = product.Key,
                        AverageRating = product.Average(p => p.Rating)
                    }).ToList();

要进行更深入的分析,您可以使用其中一个IL反编译器来查看每个提议的解决方案如何编译为IL。 对于性能分析,您可以使用StopWatch类进行测试。

答案 1 :(得分:1)

是的,您可以,在某些情况下,它会极大地影响您的查询效果。

List<TopRated> TopRated = (from review in db.ProductReviews
                        group review by review.ProductID into product
                        select new 
                        {
                          FirstProduct=product.FirstOrDefault(p=>p.Products)
                          ProductId = product.Key,
                          AverageRating = product.Average(p => p.Rating)
                        }).Select(product=>new TopRated {
                          ProductName = product.FirstProduct.Name,
                          Price = product.FirstProduct.Price,
                          PriceOffer = product.FirstProduct.PriceOffer,
                          ProductId = product.ProductId,
                          AverageRating = product.AverageRating
                        }).ToList();

答案 2 :(得分:1)

你可以这样做:

List<TopRated> TopRated = 
   (from review in db.ProductReviews
    group review by review.ProductID into product
    select new { ProductId = product.Key, Products = product.FirstOrDefault().Products, AverageRating = product.Average(p => p.Rating) } into topRated
    select new TopRated
        {
            ProductName = topRated.Products.Name,
            Price = topRated.Products.Price,
            PriceOffer = topRated.Products.PriceOffer,
            ProductId = product.Key,
            AverageRating = topRated.AverageRating
        })
   .ToList();

我很想知道这会对查询性能产生什么影响,以及方法生成的查询与此方法生成的查询之间的差异。

答案 3 :(得分:0)

您可以,但最好首先进行分组和聚合,然后通过加入获取产品信息,如下所示:

var query = (from review in db.ProductReviews
             group review by review.ProductID into reviews
             select new { ProductId = reviews.Key, AverageRating = reviews.Average(item => item.Rating) } into summary
             join product in db.Products on summary.ProductId equals product.Id
             select new TopRated
             {
                 ProductName = product.Name,
                 Price = product.Price,
                 PriceOffer = product.PriceOffer,
                 ProductId = product.Id,
                 AverageRating = summary.AverageRating
             });
var sqlQuery = query.ToString();
var result = query.ToList();

或在这种特殊情况下(只有一个聚合)甚至更简单:

var query = (from product in db.Products
             select new TopRated
             {
                 ProductName = product.Name,
                 Price = product.Price,
                 PriceOffer = product.PriceOffer,
                 ProductId = product.Id,
                 AverageRating = product.ProductReviews.Average(review => review.Rating)
             });
var sqlQuery = query.ToString();
var result = query.ToList();