Tony B

Tony B

  • 1.7k
  • 9
  • 303

How do I iterate through a List inside a C# Class?

Sep 7 2024 4:47 AM

I am pulling weather data from a Web API call which returns JSON which populates the following class:

    public class WeatherResponse
    {
        public string queryCost { get; set; }
        public string latitude { get; set; }
        public string longitude { get; set; }
        public string resolvedAddress { get; set; }
        public string address { get; set; }
        public string timezone { get; set; }
        public string tzoffset { get; set; }
        public List<DayInfo> days { get; set; }

    }

There is a List inside it: DayInfo with the daily info for a number of days (see below).

    public class DayInfo
    {
        public string datetime { get; set; }
        public string tempmax { get; set; }
        public string tempmin { get; set; }

        // etc..
    }

How do I iterate through this list in the WeatherResponse object. eg: with a foreach.
Basically I need to insert the data for each day into an MS SQL table.
Thanks.


Answers (2)