开发者

Use Where with .Select Linq

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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜