5

Sto cercando di ottenere un riepilogo degli acquisti confermati/finalizzati effettuando una query su una tabella SaleConfirmation, ma sto riscontrando molte difficoltà con la query di sintassi del metodo.Entity Framework 5 Metodo query roll up

tabella di database
Ecco la struttura della tabella SaleConfirmation che memorizza le vendite finalizzati.

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 univoco fila.
  • OfferId: chiave esterna che collega a una tabella Offerta/Vendita .
  • ProdId: ID del prodotto nella tabella prodotti.
  • Qtà: la quantità venduta al cliente.
  • Data di vendita: la data in cui la vendita è stata finalizzata.

azione controller

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

L'errore nella query di selezione è g.Sum (ii => ii.Qty) e l'errore è inferiore.

Dichiaratore membro di tipo anonimo non valido. I membri di tipo anonimo devono dichiarare con un'assegnazione membro, un nome semplice o un accesso membro.

risposta

6

Hai solo bisogno di assegnare il tipo anonimo a una variabile, prova questo.

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