Generate combinations of elements held in multiple list of strings in C#

.net, c#

Solution

Just do a cross-join with each successive list:

 IEnumerable<string> lstRes = new List<string> {null};
 foreach(var list in lstMaster)
 {
     // cross join the current result with each member of the next list
     lstRes = lstRes.SelectMany(o => list.Select(s => o + s));
 }

results:

List<String> (8 items)
------------------------ 
1-Jan-2014 
1-Jan-2015 
1-Feb-2014 
1-Feb-2015 
2-Jan-2014 
2-Jan-2015 
2-Feb-2014 
2-Feb-2015 

Notes:

Declaring `lstRes` as an `IEnumerable<string>` prevents the unnecessary creation of additional lists that will be thrown away with each iteration

The instinctual `null` is used so that the first cross-join will have something to build on (with strings, `null + s = s`)

Problem

I'm trying to automate the nested foreach provided that there is a Master List holding List of strings as items for the following scenario. Here for example I have 5 list of strings held by a master list 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 below for loop regardless of the number of list items held by the master list lstMaster

Original source