Is it possible to list all permutations of two IEnumerables using linq

I could do this with loops, but is there a way to take two IEnumerables, list all possible permutations, and select an object containing the permutation? I feel that this β€œshould” be possible, but I'm not sure which operators to use.

Thanks james

+4
source share
2 answers

Are you talking about what is basically a Cartesian compound? You can do something like

var query = from item1 in enumerable1 from item2 in enumerable2 select new { Item1 = item1, Item2 = item2 } 
+8
source

Anthony's answer is correct. Equivalent to extension method:

 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 }); ); 
+5
source

Source: https://habr.com/ru/post/1306799/


All Articles