This is known as the N-ary Cartesian Product.
Eric Lippert blogged a solution to this:
public static IEnumerable<IEnumerable<T>> Combine<T>(params IEnumerable<T>[] sequences)
{
return Combine(sequences.AsEnumerable());
}
public static IEnumerable<IEnumerable<T>> Combine<T>(IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from accseq in accumulator
from item in sequence
select accseq.Concat(new[] { item }));
}
This is how you could use it:
var array1 = new[] { 0, 2, 4, 6 };
var array2 = new[] { 0, 4, 5, 7 };
var array3 = new[] { 1, 4, 3, 5 };
var result = var result = Combine(array1, array2, array3);
var combinations = result.Select(x => x.ToArray()).ToArray();
foreach (var combination in combinations)
Console.WriteLine(string.Join(", ", combination));
Note that you don't actually need to convert the resulting enumerables into arrays like I did above, unless you need to pass them to methods that expect arrays.
You can use result
directly like so:
foreach (var combination in result)
Console.WriteLine(string.Join(", ", combination));