2011-05-03 70 views
2

我有一個LINQ查詢與超過2條件,但它似乎沒有評估超過2個條件。有沒有辦法給where子句添加更多條件?linq其中條款

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 
    }; 
+0

你能張貼一些示例代碼嗎? – tomasmcguinness 2011-05-03 15:41:55

+0

請提供一些代碼.. – 2011-05-03 15:41:57

+0

代碼,代碼。向我們展示代碼。 – 2011-05-03 15:42:05

回答

2

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 
    }; 
0

不應該有多個條件的問題。例如,你可以從Order表中得到類似的東西。

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

multiv123想要超過2個條件的答案。 – 2011-05-03 15:45:17