I need to convert this query to linq.
select CONVERT(date, tt.FechaHora)
, k.Numero
,MAX(FechaHora)
,MIN(FechaHora)
,count(case when tt.EstadoTransaccionID=1 then TransaccionID else null end)
,count(case when tt.EstadoTransaccionID=2 then TransaccionID else null end)
,(select top 1 c.Observacion from Cierre c where CONVERT(date, c.FechaHora)= CONVERT(date, tt.FechaHora))
from Transaccion tt
left join Kiosk k on k.KioskID = tt.KioskID
where TipoTransaccionID=3
and EstadoTransaccionID in (1,2)
group by CONVERT(date, tt.FechaHora), k.Numero
order by CONVERT(date, tt.FechaHora) desc;
I know I can do it by stored procedure but to not change everything I'd prefer to do it with linq
to have max min and counts with where try to do it in the following way with linq:
Monitoreos = (from ticketitem in db.Transacciones
where
(listaATM.Count() == 0 || listaATM.Contains((int)ticketitem.KioskID))
&& (ticketitem.FechaHora >= FechaDesde || FechaDesde == null)
&& (ticketitem.FechaHora <= FechaHasta || FechaHasta == null)
&& (ticketitem.TipoTransaccionID == 3)
&& (ticketitem.EstadoTransaccionID == 1 || ticketitem.EstadoTransaccionID == 2)
group ticketitem by new
{
date= DbFunctions.TruncateTime(ticketitem.FechaHora),
numero = ticketitem.Kiosk.Numero,
} into g
select new MonitoreoDTO
{
Fecha = g.FirstOrDefault().FechaHora.ToString(),
NroPos = g.FirstOrDefault().Kiosk.Numero,
FechaPrimeraOperacion = g.Max(x=> x.FechaHora).ToString(),
FechaUltimaOperacion = g.Min(x => x.FechaHora).ToString(),
CantEfectuadas = g.Count(x=> x.EstadoTransaccionID==1),
CantPendientes = g.Count(x => x.EstadoTransaccionID == 2),
Observacion = db.Cierres.Where(x => DbFunctions.TruncateTime(x.FechaHora) == g.FirstOrDefault().FechaHora).Take(1).OrderByDescending(x => x.FechaHora).FirstOrDefault().Observacion,
}).OrderByDescending(x=> x.Fecha).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
But it never ends, in case you wonder you have a pager in which it takes 25 per page to avoid having to take the whole list.
My doubt is that in SQL it does it in 1 minute but it is linq it never ends it always goes out for timeout even having 30 minutes in command timeout. I really do not know where the error is.
Greetings.