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.
LINQ Select operator is used to return an IEnumerable collection of items, including the data performed on the transformation of the method. By using Select Operator, we can shape the data as per our needs. In it, we can use two syntax types; let's see each method working flow.
LINQ Contains operator is used to check whether an element is available in sequence (collection) or not. Contains operator comes under Quantifier Operators category in LINQ Query Operators. Below is the syntax of Contains operator.
Step 1: Right-click on the Models folder in the Solution Explorer then go to "Add" and click on "Class." Step 2: Choose "LINQ to SQL Classes" from the list and provide the name "User" for the dbml name.
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.
Simply add the Where
before the Select
:
List<DTFlight> testList =
_ctrFlightList.Where(<your condition>)
.Select(i => new DTFlight() { AirLineName = i.AirLineName,
ArrivalDate = i.ArrivalDate })
.ToList();
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With