I need to find a way of condensing a large number of foreach loops down into one easy to manage function if possible.
The following example provides two loops which create combinations of their respective values for each iteration.
The problem is that there could be up to 30 loops and this has lead to unsightly code that is hard to manage.
//Setting up house instances
House house1 = new House();
house1.DoorNumber = 2;
House house2 = new House();
house2.DoorNumber = 3;
//Adding these to a list
List<House> _houseList = new List<House>();
_houseList.Add( house1 );
_houseList.Add( house2 );
//Setting up street instances
Street street1= new Street ();
street1.Name = "Wildflower Street";
Street street2 = new Street();
street2.Name = "Teras Kasi Street";
//Again adding these to a list
List<Street > _streetList = new List<Street >();
_streetList .Add( street1 );
_streetList .Add( street2 );
//Now the question, these loops will grow exponentially
foreach( House house in _houseList )
{
foreach( Street street in _streetList )
{
Address address = new Address();
address.House = house;
address.Street = street;
Console.WriteLine( address.House.DoorNumber.ToString() + " " + address.Street.StreetName );
}
}
Expected Output
2 Wildflower Street
2 Teras Kasi Street
3 Wildflower Street
3 Teras Kasi Street
The output is simply for clarification of how the Address objects should look in memory.
Please Note
The question is not a duplicate, all other questions I have seen posted on SO have asked to print and concatenate string/integers. This question asks how to add combinations of objects to properties of another class using lists.