Ho una tabella di database Transazione (transactionID, LocalAmount ...). dove datatype per la proprietà Localamount è float. Sull'interfaccia utente Sto tentando di restituire uno SUM di colonna (Localamount) in una riga in un evento clic del pulsante.La trasmissione in decimale non è supportata nelle query LINQ alle entità
ho usato decimale invece di galleggiante
Comunque sto ottenendo un errore sul codice dove sto casting per decimale
System.NotSupportedException was unhandled by user code
Message=Casting to Decimal is not supported in LINQ to Entities queries, because the required precision and scale information cannot be inferred.
Il
public static IEnumerable<TransactionTotalForProfitcenter> GetTotalTransactionsForProfitcenter(int profitcenterID)
{
List<TransactionTotalForProfitcenter> transactions = new List<TransactionTotalForProfitcenter>();
using (var context = new CostReportEntities())
{
transactions = (from t in context.Transactions
join comp in context.Companies on t.CompanyID equals comp.CompanyID
join c in context.Countries on comp.CountryID equals c.CountryID
where c.CountryID.Equals(comp.CountryID) && t.CompanyID == comp.CompanyID
join acc in context.Accounts
on t.AccountID equals acc.AccountID
join pc in context.Profitcenters
on t.ProfitcenterID equals pc.ProfitcenterID
group t by pc.ProfitcenterCode into tProfitcenter
select new TransactionTotalForProfitcenter
{
ProfitcenterCode = tProfitcenter.Key,
//the error is occurring on the following line
TotalTransactionAmount = (decimal)tProfitcenter.Sum(t => t.LocalAmount),
//the error is occurring on the following line
TotalTransactionAmountInEUR = (decimal)tProfitcenter.Sum(t => t.AmountInEUR) //the error is occurring on this line
}
).ToList();
}
return transactions;
}
Ho provato poche opzioni da i seguenti post ma senza fortuna.
Chiunque può segnalare quali altre opzioni io possa provare. Scusa la mia piccola conoscenza su LINQ se è troppo banale.
Sembra fattibile, ma sto ricevendo un errore {"Il cast specificato da un tipo 'System.Decimal' materializzato al tipo 'System.Double' non è valido."}. Questo perché la classe Model per Transaction contiene il doppio per le proprietà LocalAmount e AmountInEuro che ho modificato manualmente, ma poi restituisce un altro errore di tipo 'Edm.Double' del membro 'LocalAmount' nel tipo di lato concettuale 'CostReportModel.Transaction' non corrisponde al tipo 'System.Decimal' del membro 'LocalAmount' sul tipo di oggetto 'CostReportModel.Transaction'. – shaz
Purtroppo, questi sono problemi separati da questa query. Sembra che tu (o qualcuno della tua squadra) stia giocando con le tue entità, il che può essere pericoloso. Potresti avere bisogno di una nuova domanda ** nuova ** sui tipi che cambiano in modo sicuro nel tuo modello di dati delle entità. –
Il problema è che ho reso il tipo di dati in DB al primo float (mancanza di conoscenza), poi li ho trasformati in decimali (perché avevo un problema di formattazione). Tuttavia, il modello è stato generato automaticamente da EF da float a double.Sarebbe una buona idea generare nuovamente le Entity Classes per provare questa soluzione? – shaz