I have an ArrayList[] myList and I am trying to create a list of all the permutations of the values in the arrays.
EXAMPLE: (all values are strings)
class Program
{
static void Main(string[] args)
{
var listofInts = new List>(3);
listofInts.Add(new List{1, 2, 3});
listofInts.Add(new List { 4,5,6 });
listofInts.Add(new List { 7,8,9,10 });
var temp = CrossJoinLists(listofInts);
foreach (var l in temp)
{
foreach (var i in l)
Console.Write(i + ",");
Console.WriteLine();
}
}
private static IEnumerable> CrossJoinLists(IEnumerable> listofObjects)
{
var result = from obj in listofObjects.First()
select new List {obj};
for (var i = 1; i < listofObjects.Count(); i++)
{
var iLocal = i;
result = from obj in result
from obj2 in listofObjects.ElementAt(iLocal)
select new List(obj){ obj2 };
}
return result;
}
}