Use Where with .Select Linq

Shivi picture Shivi · Mar 17, 2011 · Viewed 56.5k times · Source

I have a scenario where i have to use .Select with where in LINQ. Below is my query.

List<DTFlight> testList = _ctrFlightList.Select(i => new DTFlight() { AirLineName = i.AirLineName,ArrivalDate = i.ArrivalDate }).ToList();

I want ti use where(add condition) to this query.

Please Help... Thanks.

Answer

Nicolas picture Nicolas · Mar 17, 2011

I suggest you this use of Where :

List<DTFlight> testList = _ctrFlightList.
    Where(ctrFlight => ctrFlight.Property > 0).
    Select(i => new DTFlight() { AirLineName = i.AirLineName, ArrivalDate = i.ArrivalDate }).ToList();

Where returns a IEnumerable, so you can apply your Select on it.