开发者

Cannot serialize interface System.Linq.IQueryable

I'm faced with an error, "Cannot serialize interface System.Linq.IQueryable." when I try to run my method in my web service. My class is as such:

        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
         开发者_如何学Python            select new AirlineSearchStrong { 
                     flightSchedule = fs,
                     flightScheduleAndPrices = fssp,
                     airline = al,
                     travelClassCapacity = altc
                     });
            return query;


    }

I've tried IQueryable, IList and returning .ToList() but most of it has turned out to be unsuccessful


i dont think you can use Iqueryable or Ienumerable as they both do lazy execution and are not serializable. The query gets executed only when you iterate through the collection.so it doesn't make sense to return the query to the caller and asking him to iterate as his end.you need to pass a List or an Array.

You may need to change the return type to List<Type>


Hows about

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

Your trying to serialize a representation of the data, the linq query itself, instead of the data resulting from executing the query, thats why it isnt working.

You need to enumerate the linq query into an enumerable set, and serialize that.

AirlineSearchStrong might need to be marked [Serializable()]

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜