如何向 LlNQ where 子句添加 2 个以上条件?

发布于 2024-11-04 10:59:15 字数 1332 浏览 1 评论 0原文

我有一个包含超过 2 个 where 条件的 LINQ 查询,但它似乎无法使用超过 2 个条件进行评估。有没有办法向 where 子句添加更多条件?

var query = 
   from f in XElement.Load(MapPath("flightdata3.xml")).Elements("flight")
   where (string)f.Element("departurelocation") == From && 
         (string)f.Element("destinationlocation") == DestCity &&
         (string)f.Element("airline") == Airline
         // && (string)f.Element("departuredate") == DepartDate && 
         // (string)f.Element("departuretime")==DepartTime
         //&& (string)f.Element("returndate")==ReturnDate && 
         //(string)f.Element("returntime")==ReturnTime
   orderby Convert.ToInt32(f.Element("price").Value)
   select new
   {
      FlightNumber = (Int32)f.Element("flightnumber"),
      Airline = (string)f.Element("airline"),
      Departure = (string)f.Element("departureairportsymbol"),
      DepartTime = (string)f.Element("departuretime"),
      Destination = (string)f.Element("destinationairportsymbol"),
      ArrivalTime = (string)f.Element("arrivaltime"),
      Stops = (int)f.Element("numberofstops"),
      Duration = (string)f.Element("duration"),
      Cabin = (string)f.Element("cabin"),
      Price = "$" + (Int32)f.Element("price"),
      ImagePath = (string)f.Element("airlineimageurl").Value
   };

I have a LINQ query with more than 2 where conditions, but it doesn't seem to evaluate with more than 2 conditions. Is there a way to add more conditions to the where clause?

var query = 
   from f in XElement.Load(MapPath("flightdata3.xml")).Elements("flight")
   where (string)f.Element("departurelocation") == From && 
         (string)f.Element("destinationlocation") == DestCity &&
         (string)f.Element("airline") == Airline
         // && (string)f.Element("departuredate") == DepartDate && 
         // (string)f.Element("departuretime")==DepartTime
         //&& (string)f.Element("returndate")==ReturnDate && 
         //(string)f.Element("returntime")==ReturnTime
   orderby Convert.ToInt32(f.Element("price").Value)
   select new
   {
      FlightNumber = (Int32)f.Element("flightnumber"),
      Airline = (string)f.Element("airline"),
      Departure = (string)f.Element("departureairportsymbol"),
      DepartTime = (string)f.Element("departuretime"),
      Destination = (string)f.Element("destinationairportsymbol"),
      ArrivalTime = (string)f.Element("arrivaltime"),
      Stops = (int)f.Element("numberofstops"),
      Duration = (string)f.Element("duration"),
      Cabin = (string)f.Element("cabin"),
      Price = "$" + (Int32)f.Element("price"),
      ImagePath = (string)f.Element("airlineimageurl").Value
   };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

预谋 2024-11-11 10:59:28

拥有多个条件应该没有问题。例如,您可以从订单表中获得类似的内容。

var orderDetails = (from o in context.OrderDetails
where o.OrderID == orderID
where o.OrderName == orderName
select o).ToList();

There shouldn't be an issue with having more then one condition. For example, you could have something like this from an Order table.

var orderDetails = (from o in context.OrderDetails
where o.OrderID == orderID
where o.OrderName == orderName
select o).ToList();
白昼 2024-11-11 10:59:24

LINQ 绝对允许两个以上的 WHERE 条件。您是否尝试过将查询分成更易于管理的部分?无论如何,LINQ 使用延迟执行,因此您不会看到这样做会造成性能损失。

您还应该考虑创建一个类来保存您要填充到结果中的信息。

public class FlightDetail
{
    public Int32 FlightNumber { get; set; }

    public String Airline { get; set; }

    public String Departure { get; set; }

    public String DepartureTime { get; set; }

    public String Destination { get; set; }

    public String ArrivalTime { get; set; }

    public Int32 Stops { get; set; }

    public String Duration { get; set; }

    public String Cabin { get; set; }

    public Int32 Price { get; set; }

    public String ImagePath { get; set; }
}

然后是这样的东西,它更具可读性,但也应该帮助您找到出现的任何错误。

var flights = 
   from f in XElement.Load(MapPath("flightdata3.xml")).Elements("flight")
   select new FlightDetail
   {
      FlightNumber = (Int32)f.Element("flightnumber"),
      Airline = (string)f.Element("airline"),
      Departure = (string)f.Element("departureairportsymbol"),
      DepartTime = (string)f.Element("departuretime"),
      Destination = (string)f.Element("destinationairportsymbol"),
      ArrivalTime = (string)f.Element("arrivaltime"),
      Stops = (int)f.Element("numberofstops"),
      Duration = (string)f.Element("duration"),
      Cabin = (string)f.Element("cabin"),
      Price = "$" + (Int32)f.Element("price"),
      ImagePath = (string)f.Element("airlineimageurl").Value
   };

var flightsByLocation = 
   flights.
   where (string)f.Element("departurelocation") == From && 
         (string)f.Element("destinationlocation") == DestCity
   select new FlightDetail
   {
      FlightNumber = (Int32)f.Element("flightnumber"),
      Airline = (string)f.Element("airline"),
      Departure = (string)f.Element("departureairportsymbol"),
      DepartTime = (string)f.Element("departuretime"),
      Destination = (string)f.Element("destinationairportsymbol"),
      ArrivalTime = (string)f.Element("arrivaltime"),
      Stops = (int)f.Element("numberofstops"),
      Duration = (string)f.Element("duration"),
      Cabin = (string)f.Element("cabin"),
      Price = "$" + (Int32)f.Element("price"),
      ImagePath = (string)f.Element("airlineimageurl").Value
   };

LINQ absolutely allows more than two WHERE conditions. Have you tried separating the query into more manageable pieces? LINQ uses deferred execution anyway so you won't see a performance penalty in doing so.

You should also consider making a class to hold the information you're stuffing into the result.

public class FlightDetail
{
    public Int32 FlightNumber { get; set; }

    public String Airline { get; set; }

    public String Departure { get; set; }

    public String DepartureTime { get; set; }

    public String Destination { get; set; }

    public String ArrivalTime { get; set; }

    public Int32 Stops { get; set; }

    public String Duration { get; set; }

    public String Cabin { get; set; }

    public Int32 Price { get; set; }

    public String ImagePath { get; set; }
}

Then something like this which is more readable but should also help you find whatever bug is popping up.

var flights = 
   from f in XElement.Load(MapPath("flightdata3.xml")).Elements("flight")
   select new FlightDetail
   {
      FlightNumber = (Int32)f.Element("flightnumber"),
      Airline = (string)f.Element("airline"),
      Departure = (string)f.Element("departureairportsymbol"),
      DepartTime = (string)f.Element("departuretime"),
      Destination = (string)f.Element("destinationairportsymbol"),
      ArrivalTime = (string)f.Element("arrivaltime"),
      Stops = (int)f.Element("numberofstops"),
      Duration = (string)f.Element("duration"),
      Cabin = (string)f.Element("cabin"),
      Price = "$" + (Int32)f.Element("price"),
      ImagePath = (string)f.Element("airlineimageurl").Value
   };

var flightsByLocation = 
   flights.
   where (string)f.Element("departurelocation") == From && 
         (string)f.Element("destinationlocation") == DestCity
   select new FlightDetail
   {
      FlightNumber = (Int32)f.Element("flightnumber"),
      Airline = (string)f.Element("airline"),
      Departure = (string)f.Element("departureairportsymbol"),
      DepartTime = (string)f.Element("departuretime"),
      Destination = (string)f.Element("destinationairportsymbol"),
      ArrivalTime = (string)f.Element("arrivaltime"),
      Stops = (int)f.Element("numberofstops"),
      Duration = (string)f.Element("duration"),
      Cabin = (string)f.Element("cabin"),
      Price = "$" + (Int32)f.Element("price"),
      ImagePath = (string)f.Element("airlineimageurl").Value
   };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文