【问题标题】:Use Where with .Select Linq将 Where 与 .Select Linq 一起使用
【发布时间】:2011-03-17 10:16:00
【问题描述】:

我有一个场景,我必须在 LINQ 中将 .Select 与 where 一起使用。 以下是我的查询。

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

我想在这个查询中使用 where(add condition)。

请帮助... 谢谢。

【问题讨论】:

    标签: linq linq-to-objects


    【解决方案1】:

    我建议你这样使用 Where :

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

    Where 返回一个 IEnumerable,因此您可以在其上应用您的 Select。

    【讨论】:

      【解决方案2】:

      只需在Select 之前添加Where

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

      【讨论】:

        【解决方案3】:

        有什么问题?

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

        例如...你需要什么条件?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-15
          • 2010-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多