Using Where with. Choose Linq

I have a script in which I should use. Choose where in LINQ. Below is my request.

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

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

Please help ... Thanks.

+6
linq linq-to-objects
source share
3 answers

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 is IEnumerable returned, so you can apply your choice on it.

+17
source share

Just add Where before Select :

 List<DTFlight> testList = _ctrFlightList.Where(<your condition>) .Select(i => new DTFlight() { AirLineName = i.AirLineName, ArrivalDate = i.ArrivalDate }) .ToList(); 
+7
source share

What is the problem?

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

for example ... What condition do you need?

+1
source share

All Articles