2
I have an appointment in LINQ
where do I use the SumAsync
, however when my entity is empty I have the exception:
The cast to value type 'System.Decimal' failed because the materialized value is null. Either the result type’s Generic Parameter or the query must use a nullable type.
The appointment I’m trying to make is this:
var valor = await db.Exemplos.SumAsync(a => a.Valor);
How can I get around that and do the SumAsync
return 0 if my entity has no data yet?
But it will be some kind of limitation of the Sumasync function that it no longer treats this kind of situation?
– Pablo Tondolo de Vargas
@Pablotondolodevargas I edited the answer to contemplate it
– Jéf Bueno
But if I do
db.Exemplos.Sum(a => a.Valor)
, I’d have to cast it like Sumasync?– Pablo Tondolo de Vargas
@Pablotondolodevargas For sure. The method should be asynchronous or not make a difference in behavior.
– Jéf Bueno