2013-03-20 23 views
5

Tôi đang cố gắng để có được một bản tóm tắt các giao dịch đã xác nhận/hoàn tất bằng cách truy vấn bảng SaleConfirmation, nhưng tôi gặp rất nhiều khó khăn với truy vấn cú pháp phương pháp.Khuôn khổ thực thể 5 Truy vấn phương thức cuộn lên

Bảng cơ sở dữ liệu
Đây là cấu trúc bảng SaleConfirmation lưu trữ doanh số cuối cùng.

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 hàng duy nhất.
  • OfferId: Khóa nước ngoài liên kết với phiếu mua hàng/Bán hàng .
  • ProdId: ID của sản phẩm trong bảng sản phẩm.
  • Số lượng: Số lượng được bán cho khách hàng.
  • SaleDate: Ngày bán hàng được hoàn thành.

điều khiển hành động

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

Các lỗi trong truy vấn chọn là g.Sum (ii => ii.Qty) và lỗi dưới đây.

Trình khai báo thành viên kiểu vô danh không hợp lệ. Thành viên loại ẩn danh phải được tuyên bố với nhiệm vụ thành viên, tên đơn giản hoặc quyền truy cập thành viên.

Trả lời

6

Bạn chỉ cần gán loại ẩn danh cho một biến, hãy thử điều này.

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 
       })); 
Các vấn đề liên quan