2011-09-23 52 views
3

我遇到了一个错误,“无法序列化接口System.Linq.IQueryable”。当我尝试在我的Web服务中运行我的方法时。我的类是这样:无法序列化接口System.Linq.IQueryable

 public class AirlineSearchStrong 
    { 
     public Flight_Schedule flightSchedule { get; set; } 
     public Flight_Schedule_Seats_and_Price flightScheduleAndPrices { get; set; } 
     public Airline airline { get; set; } 
     public Travel_Class_Capacity travelClassCapacity { get; set; } 

    } 

    [WebMethod] 
    public IQueryable SearchFlight(string dep_Date, string dep_Airport, string arr_Airport, int no_Of_Seats) 
    {   
     AirlineLinqDataContext db = new AirlineLinqDataContext(); 
     var query = (from fs in db.Flight_Schedules 
        join fssp in db.Flight_Schedule_Seats_and_Prices on fs.flight_number equals fssp.flight_number 
        join al in db.Airlines on fs.airline_code equals al.airline_code 
        join altc in db.Travel_Class_Capacities on al.aircraft_type_code equals altc.aircraft_type_code 

        where fs.departure_date == Convert.ToDateTime(dep_Date) 
        where fs.origin_airport_code == dep_Airport 
        where fs.destination_airport_code == arr_Airport 
        where altc.seat_capacity - fssp.seats_taken >= no_Of_Seats 
        select new AirlineSearchStrong { 
        flightSchedule = fs, 
        flightScheduleAndPrices = fssp, 
        airline = al, 
        travelClassCapacity = altc 
        }); 
      return query; 


    } 

我试过的IQueryable,IList的和返回.ToList(),但大部分已经被证明是不成功的

回答

2

怎么样了约

public IEnumerable<AirlineSearchStrong> SearchFlight(string dep_Date, string dep_Airport, string arr_Airport, int no_Of_Seats) 
{ 
    ... 
    return query.ToList(); 
} 

你试图序列化数据表示,linq查询本身,而不是执行查询产生的数据,这就是为什么它不工作。

您需要枚举linq查询到一个可枚举集合中,并对其进行序列化。

AirlineSearchStrong可能需要打上[Serializable()]

+0

我得到无法序列接口System.Collections.Generic.IEnumerable'1 [Travel_Reservation_System.Services.AirlineSearchService + AirlineSearchStrong,旅行预订系统,版本= 1.0.0.0 ,Culture = neutral,PublicKeyToken = null]]。作为错误消息 –

6

我不认为 可以使用的IQueryable或IEnumerable的,因为它们都做懒执行,不能序列化。查询只有在遍历集合时才会执行,所以将查询返回给调用者并要求他迭代为他的结束没有意义。您需要通过ListArray

您可能需要返回类型更改为List<Type>