I have a data from database, that I want to put into an array for later process.
php version:
$data = [];
// $data[date][id] = [name, age];
$data["09/10/2023"][10] = ['name' => 'John Smith', 'age' => 50];
$data["09/10/2023"][11] = ['name' => 'Amie Kim', 'age' => 30];
How can I write the above code in C# ?
Later I want to loop through that data.
>Solution :
Use a C# Dictionary to hold Person objects:
// Define a class to hold the name and age
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
// Then in your method or constructor
var data = new Dictionary<string, Dictionary<int, Person>>();
// Initialize the inner dictionary for the date "09/10/2023"
data["09/10/2023"] = new Dictionary<int, Person>();
// Add the persons
data["09/10/2023"][10] = new Person { Name = "John Smith", Age = 50 };
data["09/10/2023"][11] = new Person { Name = "Amie Kim", Age = 30 };