Eric Lippertの記事に基づく
void Main()
{
var set1 = new object[]{'a', 'b', 'c'};
var set2 = new object[]{1,2,};
var set3 = new object[]{'x', 'y', 'z'};
string.Join(", ", new[] {set1, set2, set3}.CartesianProduct().Select(item => item.ToArray()).Select(item => string.Format("({0},{1},{2})", item[0], item[1], item[2]))).Dump();
}
public static class CartesianProductContainer
{
public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this 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}));
}
}
出力:
(a,1,x), (a,1,y), (a,1,z), (a,2,x), (a,2,y), (a,2,z), (b,1,x), (b,1,y), (b,1,z), (b,2,x), (b,2,y), (b,2,z), (c,1,x), (c,1,y), (c,1,z), (c,2,x), (c,2,y), (c,2,z)