是否可以使用 linq 枚举两个 IEnumerable 的所有排列

发布于 2024-08-28 20:55:57 字数 110 浏览 5 评论 0原文

我可以使用循环来做到这一点,但是有没有办法获取两个 IEnumerables,枚举所有可能的排列并选择包含该排列的对象?我觉得这“应该”是可能的,但我不太确定要使用什么运算符。

谢谢 詹姆斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_畞蕅 2024-09-04 20:55:57

您是在谈论什么是笛卡尔连接?你可以做类似的事情

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

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 }
时光清浅 2024-09-04 20:55:57

安东尼的回答 是正确的。等效的扩展方法是:

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

var query = enumerable1.SelectMany(
                item1 => enumerable2.Select(item2 => 
                    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 });
            );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文