I am trying to automate a nested foreach, provided that there is a main list containing a list of strings as elements for the following scenario.
Here, for example, I have 5 lists of strings stored in the main list of lstMaster
List<string> lst1 = new List<string> { "1", "2" }; List<string> lst2 = new List<string> { "-" }; List<string> lst3 = new List<string> { "Jan", "Feb" }; List<string> lst4 = new List<string> { "-" }; List<string> lst5 = new List<string> { "2014", "2015" }; List<List<string>> lstMaster = new List<List<string>> { lst1, lst2, lst3, lst4, lst5 }; List<string> lstRes = new List<string>(); foreach (var item1 in lst1) { foreach (var item2 in lst2) { foreach (var item3 in lst3) { foreach (var item4 in lst4) { foreach (var item5 in lst5) { lstRes.Add(item1 + item2 + item3 + item4 + item5); } } } } }
I want to automate the loop below, regardless of the number of list items stored in the lstMaster main list
source share