Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 5 Method Query Roll Up

I'm trying to get a summary of confirmed/finalized purchases by querying a SaleConfirmation table, but I'm having a lot of difficulty with the method syntax query.

Database Table
Here is the SaleConfirmation table structure that stores finalized sales.

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: Unique row ID.
  • OfferId: Foreign key that links to a Offer/Sale table.
  • ProdId: ID of the product in the products table.
  • Qty: The quantity sold to the customer.
  • SaleDate: The date when the sale was finalized.

Controller Action

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

The error in the select query is g.Sum(ii => ii.Qty) and the error is below.

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

like image 780
Jason Harris Avatar asked Mar 20 '13 20:03

Jason Harris


1 Answers

You just need to assign the anonymous type to a variable, try this.

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
               }));
like image 187
Overmachine Avatar answered Oct 02 '22 15:10

Overmachine