[英]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
控制器動作
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.