linq Multiple Where based on conditions.
- by Bathan
I want to query a table with some conditions based on user input.
I wrote this :
IQueryable 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'm having is that the "where" clause gets stepped over with the last one. Whats the correct way to do something like this on LINQ?
The "helper" object is a custom class storing the user input dates for this example.