I want to query a table with some conditions based on user input.
I have this code:
IQueryable<Turno> turnoQuery = dc.Turno;
if (helper.FechaUltimaCitaDesde != DateTime.MinValue)
{
turnoQuery = turnoQuery.Where(t => t.TurnoFecha >= helper.FechaUltimaCitaDesde);
}
if (helper.FechaUltimaCitaHasta != DateTime.MinValue)
{
turnoQuery = turnoQuery.Where(t => t.TurnoFecha <= helper.FechaUltimaCitaHasta);
}
if (helper.SoloCitasConsumidas)
{
turnoQuery = turnoQuery.Where(t => t.Estado == Convert.ToInt32(EnmEstadoDelTurno.Consumido));
}
else if(helper.AnuladoresDeCitas)
{
turnoQuery = turnoQuery.Where(t => t.Estado == Convert.ToInt32(EnmEstadoDelTurno.Cancelado) || t.Estado == Convert.ToInt32(EnmEstadoDelTurno.Ausente));
}
The problem I am facing is that the where clause is overwritten last.
What is the correct way to do something like this in LINQ?
The helper object is a user class that stores user input dates for this example.
source
share