Cannot implicitly convert type System.Collections.Generic.List to System.Collections.Generic.IEnumerable

Jassim Al Rahma 1,616 Reputation points
2022-07-12T18:14:53.787+00:00

Hi,

I am trying to consume a web service to display Pins on Syncfusion Maps but getting this error:

Error CS0266: Cannot implicitly convert type 'System.Collections.Generic.List<Skyye.MainPage.LiveFlights.Response>' to 'System.Collections.Generic.IEnumerable<Syncfusion.SfMaps.XForms.MapMarker>'. An explicit conversion exists (are you missing a cast?)

Here is my class:

class LiveFlights  
{  
    public class Agent  
    {  
    }  
  
    public class Client  
    {  
        public string ip { get; set; }  
        public Geo geo { get; set; }  
        public Connection connection { get; set; }  
        public Device device { get; set; }  
        public Agent agent { get; set; }  
        public Karma karma { get; set; }  
    }  
  
    public class Connection  
    {  
        public string type { get; set; }  
        public int isp_code { get; set; }  
        public string isp_name { get; set; }  
    }  
  
    public class Device  
    {  
    }  
  
    public class Geo  
    {  
        public string country_code { get; set; }  
        public string country { get; set; }  
        public string continent { get; set; }  
        public string city { get; set; }  
        public double lat { get; set; }  
        public double lng { get; set; }  
        public string timezone { get; set; }  
    }  
  
    public class Karma  
    {  
        public bool is_blocked { get; set; }  
        public bool is_crawler { get; set; }  
        public bool is_bot { get; set; }  
        public bool is_friend { get; set; }  
        public bool is_regular { get; set; }  
    }  
  
    public class Key  
    {  
        public int id { get; set; }  
        public string api_key { get; set; }  
        public string type { get; set; }  
        public DateTime expired { get; set; }  
        public DateTime registered { get; set; }  
        public int limits_by_hour { get; set; }  
        public int limits_by_minute { get; set; }  
        public int limits_by_month { get; set; }  
        public int limits_total { get; set; }  
    }  
  
    public class Params  
    {  
        public string lang { get; set; }  
    }  
  
    public class Request  
    {  
        public string lang { get; set; }  
        public string currency { get; set; }  
        public int time { get; set; }  
        public string id { get; set; }  
        public string server { get; set; }  
        public string host { get; set; }  
        public int pid { get; set; }  
        public Key key { get; set; }  
        public Params @params { get; set; }  
        public int version { get; set; }  
        public string method { get; set; }  
        public Client client { get; set; }  
    }  
  
    public class Response  
    {  
        public string hex { get; set; }  
        public string flag { get; set; }  
        public double lat { get; set; }  
        public double lng { get; set; }  
        public int alt { get; set; }  
        public int dir { get; set; }  
        public int speed { get; set; }  
        public double v_speed { get; set; }  
        public string flight_number { get; set; }  
        public string flight_icao { get; set; }  
        public string flight_iata { get; set; }  
        public string dep_icao { get; set; }  
        public string dep_iata { get; set; }  
        public string airline_icao { get; set; }  
        public string airline_iata { get; set; }  
        public int updated { get; set; }  
        public string status { get; set; }  
        public string reg_number { get; set; }  
        public string arr_icao { get; set; }  
        public string arr_iata { get; set; }  
        public string aircraft_icao { get; set; }  
  
        /*  
        public Position position  
        {  
            get  
            {  
                return new Position(lat, lng);  
            }  
        }  
        */  
    }  
  
    public class Root  
    {  
        public Request request { get; set; }  
        public List<Response> response { get; set; }  
        public string terms { get; set; }  
    }  
}  

and this is my C#:

var client = new HttpClient();  
  
client.BaseAddress = new Uri("https://airlabs.co/api/v9/flights");  
  
var content = new FormUrlEncodedContent(new[]  
{  
    new KeyValuePair<string, string>("api_key", "xxxxxx-xxxxx-xxxx-xxxxx-xxxxx"),  
});  
  
var response = await client.PostAsync("https://airlabs.co/api/v9/flights", content);  
  
if (response.IsSuccessStatusCode)  
{  
    var result = await response.Content.ReadAsStringAsync();  
  
    List<LiveFlights.Root> data = JsonSerializer.Deserialize<List<LiveFlights.Root>>(result);  
  
    MapContactLocationLayer.MarkerItemsSource = data[0].response;      
}  
else  
{  
    await App.Current.MainPage.DisplayAlert("Error", "Error", "Error");  
}  

Kindly help..

Thanks,
Jassim

Developer technologies .NET Xamarin
{count} votes

Accepted answer
  1. Anonymous
    2022-07-13T09:42:03.953+00:00

    Hello,​

    Please change this line List<LiveFlights.Root> data = JsonSerializer.Deserialize<List<LiveFlights.Root>>(result); to Root data = JsonConvert.DeserializeObject<Root>(result);.

    And I tested your flights.txt file, I found value of "api_key" is garbled, so I change value to "api_key": "test" for testing.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.