Estoy intentando traducir un SQL Query a Lambda, tengo la mayoría traducido pero cuando intento sumar los valores de unos de los campos que intento seleccionar, no los suma. Antes de sumar los valores, les hago un Math.Round()
para redondear los valores y posterior se debe hacer una suma.
He estado utilizando las siguientes preguntas como referencia pero aun así no suma los valores:
obtenet campo de informacion en select distinct sum Linq
¿Como sumar/agrupar 2 filas con LinQ en C#?
anexo el query que estoy traduciendo
select format(r.req_fecha,'MMM') Mes, round(sum(t.TIC_Cobrable) / 60,2) [Horas consumidas]
from Requerimiento r inner join Ticket t on r.REQ_Id = t.REQ_Id
where r.CLI_Id = user and r.ERE_Id = id2 and r.REQ_Fecha between '01-JAN-2021' and '31-MAY-2021'
group by format(r.req_fecha,'MMM')
/*
Resultado esperado
Mes Horas consumidas
Apr 0
Feb 2.75
*/
El query anterior agrupa por Fecha pero no exactamente, mas bien obtengo solo el mes de la fecha para poder agruparlo por el mes, asi es como da el Resultado esperado
pero cuando cuando lo paso a lambda hace todo bien excepto cuando trato de agrupar formateado por mes el campo REQ_Fecha
y obtengo el siguiente error:
System.InvalidOperationException: The LINQ expression 'DbSet<Requerimiento>()
.Join(
inner: DbSet<Ticket>(),
outerKeySelector: r => r.REQ_Id,
innerKeySelector: t => t.REQ_Id,
resultSelector: (r, t) => new TransparentIdentifier<Requerimiento, Ticket>(
Outer = r,
Inner = t
))
.Where(ti => ti.Outer.CLI_Id == __user_0 && ti.Outer.ERE_Id == 2 && ti.Outer.REQ_Fecha >= 01/01/2021 12:00:00 AM && ti.Outer.REQ_Fecha <= 05/31/2021 12:00:00 AM)
.GroupBy(
keySelector: ti => Convert.ToDateTime(ti.Outer.REQ_Fecha).ToString("MMM"),
elementSelector: ti => new {
r = ti.Outer,
t = ti.Inner
})' could not be translated. Additional information: Translation of method 'System.Convert.ToDateTime' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information.
Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
Anexo el lambda
var consumibleHoursByMonth = _db.Requerimiento.Join(_db.Ticket, r => r.REQ_Id, t => t.REQ_Id, (r, t) => new { r, t })
.Where(temp => temp.r.CLI_Id == user && temp.r.ERE_Id == id2 && temp.r.REQ_Fecha >= Convert.ToDateTime("01-JAN-2021") && temp.r.REQ_Fecha <= Convert.ToDateTime("31-MAY-2021"))
.GroupBy(temp => Convert.ToDateTime(temp.r.REQ_Fecha).ToString("MMM"))
.Select(g => new { REQ_Fecha = g.Key.ToString("MMM"), TIC_Cobrable = Math.Round(g.Sum(temp => temp.t.TIC_Cobrable) / 60, 2, MidpointRounding.AwayFromZero) });
return Json(new { dataconsumiblehours = consumibleHoursByMonth });
/*
Resultado obtenido
{reQ_Fecha: "Feb", tiC_Cobrable: 0.5}
{reQ_Fecha: "Feb", tiC_Cobrable: 0.75}
{reQ_Fecha: "Feb", tiC_Cobrable: 1.5}
{reQ_Fecha: "Feb", tiC_Cobrable: 0}
{reQ_Fecha: "Apr", tiC_Cobrable: 0}
*/
Hay alguna forma de agruparlo pero SOLAMENTE formateado por mes? es decir REQ_Fecha.ToString("MMM")
en el GroupBy
?
Gracias de antemano
.Sum()
no me regresa lo del query original si no mas bien me regresa los valores separados