使用Where.选择Linq

Shi*_*ivi 9 linq linq-to-objects

我有一个我必须使用的场景.选择LINQ中的位置.以下是我的查询.

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

我希望在这个查询中使用where(添加条件).

请帮忙......谢谢.

Nic*_*las 21

我建议你使用Where:

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

返回IEnumerable的位置,因此您可以在其上应用Select.


Dan*_*rth 7

只需在Where之前添加Select:

List<DTFlight> testList =
    _ctrFlightList.Where(<your condition>)
                  .Select(i => new DTFlight() { AirLineName = i.AirLineName,
                                                ArrivalDate = i.ArrivalDate })
                  .ToList();
Run Code Online (Sandbox Code Playgroud)