views:

83

answers:

2

I could do this using loops, but is there a way to take two IEnumerables, enumerate through all possible permutations and select an object that contains the permutation? I feel like this 'should' be possible but am not really sure what operators to use.

Thanks James

+8  A: 

Are you talking about what is basically a cartesian join? You can do something like

var query = from item1 in enumerable1
            from item2 in enumerable2
            select new { Item1 = item1, Item2 = item2 }
Anthony Pegram
This is probably the way to go to get all permutations - just keep in mind it is O(n*m). However, if you want just a single permutations (say a random one), you are better off using `ElementAt()` on each list with a random index to produce that permutations.
LBushkin
+4  A: 

Anthony's answer is correct. The extension method equivalent is:

var query = enumerable1.SelectMany(
                x => enumerable2,
                (item1, item2) => new { Item1 = item1, Item2 = item2 }
            );

or

var query = enumerable1.SelectMany(
                item1 => enumerable2.Select(item2 => 
                    new { Item1 = item1, Item2 = item2 });
            );
Richard Szalay