2015-10-09 70 views
2

我的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(), 

我認爲我通過做類似上述的「增加超載」。

+3

不,不是,你不能。這些線條相似也沒有錯。 – Jon

+0

你能展示你的模型的定義嗎? (例如ProductReview,Product ..) –

+0

您可以使用String.Join()方法獲取單個字符串。也許你真的想把結果放到數據表中。 – jdweng

回答

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類進行測試。

+0

打了幾分鐘;) –

+0

@GertArnold是的,這樣的事情現在和然後:) –

+0

哦,很好。我總是忘記'let',因爲我傾向於避免查詢語法;我更喜歡自己的方法語法。我會給你一個。 –

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(); 
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(); 

我會很好奇地想知道這有什麼作用,對查詢性能以及通過你的方法和查詢生成的查詢之間的區別由此方法生成。

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();