簡體   English   中英

無法序列化System.Linq.IQueryable接口

[英]Cannot serialize interface System.Linq.IQueryable

我遇到了一個錯誤,“無法序列化System.Linq.IQueryable接口。” 當我嘗試在我的Web服務中運行我的方法時。 我的班級是這樣的:

        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(),但大部分都證明是不成功的

我不認為你可以使用Iqueryable或Ienumerable,因為它們都執行延遲執行並且不可序列化。 只有在遍歷集合時才會執行查詢。因此將查詢返回給調用者並要求他作為結束進行迭代是沒有意義的。您需要傳遞ListArray

您可能需要將返回類型更改為List<Type>

怎么樣

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

你試圖序列化數據的表示,linq查詢本身,而不是執行查詢產生的數據,這就是為什么它不工作。

您需要將linq查詢枚舉到可枚舉集中,並對其進行序列化。

AirlineSearchStrong可能需要標記為[Serializable()]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM