开发者

Is it possible to enumerate for all permutations of two IEnumerables using linq

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开发者_如何学JAVA 'should' be possible but am not really sure what operators to use.

Thanks James


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'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 });
            );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜