簡體   English   中英

實體框架5方法查詢匯總

[英]Entity Framework 5 Method Query Roll Up

我試圖通過查詢SaleConfirmation表來獲得已確認/已完成購買的摘要,但是我在方法語法查詢方面遇到很多困難。

數據庫表
這是存儲最終銷售的SaleConfirmation表結構。

Id   OfferId   ProdId      Qty    SaleDate
-------------------------------------------------------
10   7         121518      150    2013-03-14 00:00:00.000
19   7         100518      35     2013-03-18 14:46:34.287
20   7         121518      805    2013-03-19 13:03:34.023
21   10        131541      10     2013-03-20 08:34:40.287
  • ID:唯一的行ID。
  • OfferId:鏈接到“要約/銷售”表的外鍵。
  • ProdId:產品表中產品的ID。
  • 數量:出售給客戶的數量。
  • SaleDate:銷售完成的日期。

控制器動作

var confRollUps = db.SaleConfirmation
                  .GroupBy(c => c.OfferId) // Ensure we get a list of unique/distinct offers
                  .Select(g => g.Select(i => new {
                            i.OfferId, 
                            i.Product.Variety, // "Category" of product, will be the same across products for this offer. i.Product is a SQL Server Navigation property.
                            i.Offer.Price, // The price of the product, set per offer. i.Offer is a SQL Server Navigation property.
                            i.Offer.Quantity, // The quantity of items that are expected to be sold before the offer expires
                            i.Offer.DateClose, // Date of when the offer expires
                            g.Sum(ii => ii.Qty) // Sum up the Qty column, we don't care about ProdIds not matching
                   }));

選擇查詢中的錯誤是g.Sum(ii => ii.Qty) ,錯誤在下面。

無效的匿名類型成員聲明符。 必須使用成員分配,簡單名稱或成員訪問來聲明匿名類型成員。

您只需要將匿名類型分配給變量,請嘗試此操作。

var confRollUps = db.SaleConfirmation
              .GroupBy(c => c.OfferId) // Ensure we get a list of unique/distinct offers
              .Select(g => g.Select(i => new {
                        OfferId = i.OfferId, 
                        ProductVariety = i.Product.Variety, // "Category" of product, will be the same across products for this offer. i.Product is a SQL Server Navigation property.
                        OfferPrice = i.Offer.Price, // The price of the product, set per offer. i.Offer is a SQL Server Navigation property.
                        OfferQty = i.Offer.Quantity, // The quantity of items that are expected to be sold before the offer expires
                        OfferDateClose =i.Offer.DateClose, // Date of when the offer expires
                        Total =g.Sum(ii => ii.Qty) // Sum up the Qty column, we don't care about ProdIds not matching
               }));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM