I'm trying to figure out how I can add an object list property to an existing object, which also has a list object property. I have 3 classes as FirstList, SecondList, and ThirdList with an Id and Text Property. ThirdList has a List Property of SecondList and SecondList has a List Property of FirstList. I've tried nesting .Add() but I can't get that to work.
//Classes
public class FirstList
{
public int Id { get; set; }
public string Text { get; set; }
}
public class SecondList
{
public int Id { get; set; }
public string Text { get; set; }
public List<FirstList> Firsts { get; set; } = new List<FirstList>();
}
public class ThirdList
{
public int Id { get; set; }
public string Text { get; set; }
public List<SecondList> Seconds { get; set; } = new List<SecondList>();
}
// program
ThirdList item = new ThirdList();
item.Id = 30;
item.Text = "Third Text";
item.Seconds.Add(new SecondList
{
Id = 20,
Text = "Second Text",
Firsts.Add(new FirstList //Trying to add a list object within the .Add method
{
Id = 10,
Text = "First Text"
})
});
// If I remove the 'Firsts' property from 'Seconds'.Add method and do the following below, I can
// add it afterwords. But, I was trying to see if it all could be added at once.
// item.Seconds[0].Firsts.Add(new FirstList
// {
// Id = 10,
// Text = "First Text"
// });