I am pulling weather data from a Web API call which returns JSON which populates the following class:
C#
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 days { get; set; }
}There is a List inside it: DayInfo with the daily info for a number of days (see below).
C#
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.

Aman GuptaRank #193 · 12.4K reputationPosted Sep 7, 2024, 5:35 AM EDT
Hi Tony,
To iterate through the List inside the WeatherResponse object and insert the data for each day into an MS SQL table, you can use a foreach loop. Here's a general outline of how to achieve this, including the SQL insertion logic:
You can use this process.
using System; using System.Collections.Generic; using System.Data.SqlClient; 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 days { get; set; } } public class DayInfo { public string datetime { get; set; } public string tempmax { get; set; } public string tempmin { get; set; } } public class Program { static void Main(string[] args) { // Assuming weatherData is populated from your API call WeatherResponse weatherData = GetWeatherDataFromAPI(); // SQL Connection String string connectionString = "your_connection_string_here"; // Insert each day's data into the MS SQL table using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); foreach (DayInfo day in weatherData.days) { string insertQuery = @" INSERT INTO WeatherTable (DateTime, TempMax, TempMin) VALUES (@DateTime, @TempMax, @TempMin)"; using (SqlCommand cmd = new SqlCommand(insertQuery, conn)) { cmd.Parameters.AddWithValue("@DateTime", day.datetime); cmd.Parameters.AddWithValue("@TempMax", day.tempmax); cmd.Parameters.AddWithValue("@TempMin", day.tempmin); cmd.ExecuteNonQuery(); } } } } static WeatherResponse GetWeatherDataFromAPI() { // Your logic to get weather data from API and deserialize into WeatherResponse object return new WeatherResponse { // Sample data for testing days = new List { new DayInfo { datetime = "2024-09-01", tempmax = "30", tempmin = "20" }, new DayInfo { datetime = "2024-09-02", tempmax = "28", tempmin = "18" } } }; } }Make sure to modify the connection string and table name according to your setup.
Uday DodiyaRank #239 · 10.3K reputationPosted Sep 7, 2024, 5:05 AM EDT
Hi, Tony B,
Try Following
using System; using System.Collections.Generic; using System.Data.SqlClient; public class Program { public static void Main() { // Call API WeatherResponse weatherResponse = GetWeatherResponseFromAPI(); // Loop through each DayInfo in the days list foreach (DayInfo day in weatherResponse.days) { // Insert each day's data into MS SQL table InsertDayInfoIntoDatabase(day); } } public static void InsertDayInfoIntoDatabase(DayInfo dayInfo) { string connectionString = "your-connection-string-here"; string query = "INSERT INTO WeatherData (Date, TempMax, TempMin) VALUES (@Date, @TempMax, @TempMin)"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(query, connection); command.Parameters.AddWithValue("@Date", dayInfo.datetime); command.Parameters.AddWithValue("@TempMax", dayInfo.tempmax); command.Parameters.AddWithValue("@TempMin", dayInfo.tempmin); connection.Open(); command.ExecuteNonQuery(); connection.Close(); } } public static WeatherResponse GetWeatherResponseFromAPI() { // Demo API Response return new WeatherResponse { days = new List { new DayInfo { datetime = "2024-09-07", tempmax = "30", tempmin = "20" }, new DayInfo { datetime = "2024-09-08", tempmax = "32", tempmin = "22" } } }; } } 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 days { get; set; } } public class DayInfo { public string datetime { get; set; } public string tempmax { get; set; } public string tempmin { get; set; } }