I have the following query in linq
using (var context = new BusinessContext())
{
var result = (from m in context.MovimientoCajas
where DbFunctions.TruncateTime(m.Fecha) == DbFunctions.TruncateTime(fecha)
&& m.PuntoEmisionId == puntoEmision && m.TipoMovimiento == MovimientoCajaType.Ingreso
select m.Ingreso).Sum();
return result;
}
The query works fine but when there is no data on that date because nothing has been entered it still gives me the following error.
The conversion to value of type 'System.Decimal' failed because the value materialized is null. The generic parameter of the result type or the query must use a type that may contain nulls.
I understand the problem, my question is whether it could validate in the same line of select select m.Ingreso).Sum();
or necessarily I have to do select new
and cast it in a class and map it to a property of decimal type. What is the most advisable?