linq Multiple Where based on conditions.

Posted by Bathan on Stack Overflow See other posts from Stack Overflow or by Bathan
Published on 2010-05-20T17:08:31Z Indexed on 2010/05/20 17:10 UTC
Read the original article Hit count: 217

Filed under:

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.

© Stack Overflow or respective owner

Related posts about plinqo