如何在Linq中合并多个相同或不同长度的列表? [英] How to combine multiple lists of same or different lengths in Linq?

查看:134
本文介绍了如何在Linq中合并多个相同或不同长度的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个方法或扩展方法,该方法或扩展方法采用多个列表,并通过以下方式将它们组合:

I would like to build a method or extension method that takes multiple lists and combines them in the following way:

假设我有两个列表:

        int[] list1 =  {3, 1, 2};
        int[] list2 =  {5, 4 };

我希望这样得到一个数组列表:

I would expect a list of arrays as a result like this:

[1,4]
[1,5]
[2,4]
[2,5]
[3,4]
[3,5]

我得到的数组列表中的列数将由数量决定 传递的列表数,并且两列都需要排序. 行数就是(列表A的长度)*(列表B的长度)*(列表N的长度)

The number of columns in my resulting list of arrays would be determined by the amount of lists passed and both columns need to be sorted. The number of rows is just the (length of list A) * (length of list B) * (length of list N)

在此示例中为3 * 2 = 6行. 2列(因为有2个输入列表).

In this example is 3 * 2 = 6 rows. 2 columns (because 2 input lists).

用linq做到这一点的优雅方法是什么?

What would be an elegant way of doing this with linq?

谢谢!

推荐答案

尝试交叉加入

int[] list1 =  {3, 1, 2};
int[] list2 =  {5, 4 }; 

var result = (from l1 in list1
             from l2 in list2
             select new [] {l1, l2}).ToList()

这篇关于如何在Linq中合并多个相同或不同长度的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆