none
Cannot convert to System.Collections.Generic.IEnumerable RRS feed

  • Question

  • Hi,

    I am trying to populate data from OpenWeatherMap using below code but getting this error:

    Error CS1503 Argument 1: cannot convert from 'Samana.WeatherInfo.List' to 'System.Collections.Generic.IEnumerable<Samana.WeatherInfo.List>'

    can anyone help please..

    This is my code:

    private async void Get5DaysForecast()
    {
        var location_data = await GetLocation();
    
        string url = "https://api.openweathermap.org/data/2.5/forecast?lat=" + location_data.latitude + "&lon=" + location_data.longitude + "&appid=" + api_key;
    
        var client = new HttpClient();
        client.BaseAddress = new Uri(url);
        var response = await client.GetAsync(url);
        var data = await response.Content.ReadAsStringAsync();
        var result = JsonConvert.DeserializeObject<WeatherInfo.List>(data);
        ObservableCollection<WeatherInfo.List> trends = new ObservableCollection<WeatherInfo.List>(result);
    }
    

    and the WeatherInfo is here:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Samana
    {
        class WeatherInfo
        {
            public class Coord
            {
                public double lon { get; set; }
                public double lat { get; set; }
            }
    
            public class Weather
            {
                public int id { get; set; }
                public string main { get; set; }
                public string description { get; set; }
                public string icon { get; set; }
            }
    
            public class List
            {
                public int dt { get; set; }
                public Main main { get; set; }
                public List<Weather> weather { get; set; }
                public Clouds clouds { get; set; }
                public Wind wind { get; set; }
                public Sys sys { get; set; }
                public string dt_txt { get; set; }
                public Rain rain { get; set; }
                public Snow snow { get; set; }
            }
    
            public class City
            {
                public int id { get; set; }
                public string name { get; set; }
                public Coord coord { get; set; }
                public string country { get; set; }
            }
    
            public class Main
            {
                public double temp { get; set; }
                public double temp_min { get; set; }
                public double temp_max { get; set; }
                public double pressure { get; set; }
                public double sea_level { get; set; }
                public double grnd_level { get; set; }
                public int humidity { get; set; }
                public double temp_kf { get; set; }
            }
    
            public class Wind
            {
                public double speed { get; set; }
                public double deg { get; set; }
            }
    
            public class Clouds
            {
                public int all { get; set; }
            }
    
            public class Sys
            {
                public double message { get; set; }
                public string country { get; set; }
                public int sunrise { get; set; }
                public int sunset { get; set; }
                public string pod { get; set; }
            }
    
            public class Rain
            {
                public double __invalid_name__3h { get; set; }
            }
    
            public class Snow
            {
                public double __invalid_name__3h { get; set; }
            }
    
            public class Root
            {
                public double message { get; set; }
                public int cnt { get; set; }
                public List<List> list { get; set; }
                public City city { get; set; }
                public Coord coord { get; set; }
                public List<Weather> weather { get; set; }
                public string @base { get; set; }
                public Main main { get; set; }
                public Wind wind { get; set; }
                public Clouds clouds { get; set; }
                public int dt { get; set; }
                public Sys sys { get; set; }
                public int id { get; set; }
                public string name { get; set; }
                public string cod { get; set; }
            }
        }
    }
    

    Wednesday, April 24, 2019 9:09 PM

Answers

  • Maybe you should use this?

     

       var result = JsonConvert.DeserializeObject<WeatherInfo.Root>( data );

       ObservableCollection<WeatherInfo.List> trends = new ObservableCollection<WeatherInfo.List>( result.list );

    • Marked as answer by Jassim Rahma Thursday, April 25, 2019 6:30 PM
    Thursday, April 25, 2019 4:24 PM

All replies

  • Try add the following inside your WeatherInfo class:

    public System.Collections.Generic.List<Samana.WeatherInfo.List> List { get; set; }

    Basically your problem is that, you're just declaring a lot of class schema inside WeatherInfo without declaring any variable.


    P.S.: I'll avoid setting type name of any class as "List" to avoid name collision with System.Collections.Generic.List, so you can type fewer character in the future.
    • Edited by cheong00Editor Thursday, April 25, 2019 1:55 AM change wording for clarification
    Thursday, April 25, 2019 1:51 AM
    Answerer
  • Maybe you should use this?

     

       var result = JsonConvert.DeserializeObject<WeatherInfo.Root>( data);

       ObservableCollection<WeatherInfo.List> trends = new ObservableCollection<WeatherInfo.List>( result.list);



    • Edited by Viorel_MVP Thursday, April 25, 2019 5:41 AM
    Thursday, April 25, 2019 5:40 AM
  • Oh, you make me realized I read the question in a wrong way.

    Possibly his code should look like this:

    ObservableCollection<WeatherInfo.List> trends = new ObservableCollection<WeatherInfo.List>(){ result };


    Thursday, April 25, 2019 5:55 AM
    Answerer
  • Oh, you make me realized I read the question in a wrong way.

    Possibly his code should look like this:

    ObservableCollection<WeatherInfo.List> trends = new ObservableCollection<WeatherInfo.List>(){ result };


    I tried this, not throwing error but not showing result... :(

    Here is the full code:

    var location_data = await GetLocation();
    
    string url = "https://api.openweathermap.org/data/2.5/forecast?lat=" + location_data.latitude + "&lon=" + location_data.longitude + "&appid=" + api_key;
    
    var client = new HttpClient();
    
    client.BaseAddress = new Uri(url);
    
    var response = await client.GetAsync(url);
    
    var data = await response.Content.ReadAsStringAsync();
                
    var result = JsonConvert.DeserializeObject<WeatherInfo.List>(data);
    
    // ObservableCollection<WeatherInfo.List> trends = new ObservableCollection<WeatherInfo.List>();
    
    ObservableCollection<WeatherInfo.List> trends = new ObservableCollection<WeatherInfo.List>() { result };
    
    ListViewWeatherForecast.ItemsSource = trends;
    

    Thursday, April 25, 2019 3:54 PM
  • Maybe you should use this?

     

       var result = JsonConvert.DeserializeObject<WeatherInfo.Root>( data );

       ObservableCollection<WeatherInfo.List> trends = new ObservableCollection<WeatherInfo.List>( result.list );

    • Marked as answer by Jassim Rahma Thursday, April 25, 2019 6:30 PM
    Thursday, April 25, 2019 4:24 PM