Possible duplicate:
Creating all possible combinations
Is there a LINQ way for a Cartesian product? How to generate a combination of N elements with a limited margin of 2 without explicit nested loops
I have a list of lists, and I want to iterate over all possible combinations when I select one item from each internal list. It is quite simple if I know at compile time how many lists there are, but how can I do this when I do not know in advance how many lists there will be?
If I have three lists (and if at compile time I know that there will be exactly three lists), and I want all combinations of selecting one element from each of the three lists I can do easily with LINQ query:
var list1 = new[] { 1, 2 };
var list2 = new[] { 3, 4 };
var list3 = new[] { 5, 6 };
var combinations = from item1 in list1
from item2 in list2
from item3 in list3
select new[] { item1, item2, item3 };
, , ?
var lists = new[] {
new[] { 1, 2 },
new[] { 3, 4 },
new[] { 5, 6 } };
var combinations = ???;
, LINQ - SelectMany foreach, SelectMany-, SelectMany. -. , . , . , SelectMany .
, , ?
(: , , IEnumerable<T> . , , , , IEnumerable<IEnumerable<int>>, int[][], .)