How do I iterate through a List inside a class?

OgrePyknic

New member
Joined
Sep 23, 2024
Messages
1
Programming Experience
Beginner
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<DayInfo> 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.
 
You would iterate over it the same way you iterate over any other list. Having a list reference as a local variable is the same as having a list reference that is a member of class or struct.

Is the real issue about how to structure the data in your database? Have you found yourself in your first encounter with the problem of stumbled of object-relational impedance mismatch?
 
In C#, for example, you can use a foreach loop to iterate over a List<T> collection. In this example, we have a List<int> called numbers that contain five integers. We then use a foreach loop to iterate over each number list item.

Traversing an Array of Gender Using a Foreach Loop​

Traversing an array of gender using a foreach loop is a common practice in programming. This method is beneficial when dealing with arrays that contain a list of genders. The foreach loop is used to iterate over each element in the array and perform a specific action, such as displaying it on the screen or storing it in a variable.

Traversing an array of gender using a foreach loop involves declaring a variable to hold each element in the array and then using the foreach loop to iterate over the array. As each element is encountered, it is stored in the variable, and the loop continues to iterate until all elements in the array have been processed. This method is widely used in programming and is an essential skill for any developer who works with arrays.

Foreach Loop With List (Collection)​

A foreach loop is a standard loop structure used in programming that allows you to iterate through the elements of a collection. For example, when working with lists in C#, a foreach loop can be handy. A list is a collection type that allows you to store and manipulate related items. Using a foreach loop with a list lets you quickly access and work with each item without worrying about the underlying implementation details.

The foreach loop is handy when you need to operate on each item in a collection.

In C#, for example, you can use a foreach loop to iterate over a List<T> collection. The loop will continue until all items in the collection have been processed.
To use a foreach loop with a list in C#, you need to declare the loop variable, which is typically the same type as the items on the list. Then, you can use the loop variable to access each item in the list, one at a time. This approach is often much more straightforward and concise than a traditional for loop, requiring you to manage the loop counter and index variables manually.

Here is an example:
Iterate Through List Inside Class:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

foreach (int number in numbers)

{

    Console.WriteLine(number);

}
 
Another Example: Finding Char in String:
Foreach can also be used to check whether a certain character is present in a given string.

using System;

class loopworkseg1

{

   static public void Main()

    {

        //reading the string

        string s = "Zahwah Jameel";         

        //converting string to array with characters as items

        char[] chArray = s.ToCharArray();

        foreach (char ch in chArray) 

        { 

            if (ch.ToString() != " ") 

            Console.WriteLine(ch); 

        }

    }

}
 
@Rythorian : The OP was not asking how to loop over a collection that they readily have access to like in all your examples.. They were asking about how to loop over a collection that happens to live inside another object.
 

Latest posts

Back
Top Bottom