Should I always need to use FirstOrDefault to show columns not included in aggregate function?
var creditos = from c in context.creditos
where c.Status == status
join a in context.acreditados on c.IDCredito equals a.IDCredito
group a by a.IDCredito into g
select new
{
Id = g.FirstOrDefault().creditos.IDCredito,
Expediente = g.FirstOrDefault().creditos.Expediente,
Status = (_Credito.Status)g.FirstOrDefault().creditos.Status,
Producto = (_Credito.Producto)g.FirstOrDefault().creditos.Producto,
Monto = g.Sum(Monto => Monto.Cantidad),
Fecha = g.FirstOrDefault().creditos.FechaInicio,
Tasa = g.FirstOrDefault().creditos.TasaInteres,
Plazo = g.FirstOrDefault().creditos.Plazo,
Periodo = g.FirstOrDefault().creditos.Periodo
};
No, not really: you could use
LastOrDefault. The important thing is to get a single value to “pair up” with scalars produced in the aggregation.An alternative is to put these values in the key at the group by stage: if you know they would be the same, add them to the key of the group, and pull them from there during the aggregation stage.