Say I have a data structure of IEnumerable
like this:
{
{ A, B }
{ 1, 2, 3 }
{ Z }
}
You could use CartesianProduct
method by Eric Lippert for this (taken from here):
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}));
}
private static IEnumerable<IEnumerable<object>> GetAllCombinations(IEnumerable<IEnumerable<object>> a)
{
if (!a.Skip(1).Any())
{
return a.First().Select(x => new[] { x });
}
var tail = GetAllCombinations(a.Skip(1)).ToArray();
return a.First().SelectMany(f => tail.Select(x => new[] { f }.Concat(x)));
}