如何使用LINQ组合两个列表?

时间:2022-12-20 20:24:11

Env.: .NET4 C#

Env。:.NET4 c#

Hi All,

你好,

I want to combine these 2 lists : { "A", "B", "C", "D" } and { "1", "2", "3" }

我想把这两个列表:{“A”、“B”、“C”,“D”}和{“1”、“2”、“3”}

into this one:

为这一个:

{ "A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3", "D1", "D2", "D3" }

Obviously, i could use nested loops. But I wonder if LINQ can help. As far as I understand, Zip() is not my friend in this case, right?

显然,我可以使用嵌套循环。但我不知道LINQ是否能帮上忙。据我所知,Zip()在这种情况下不是我的朋友,对吗?

TIA,

蒂雅,

3 个解决方案

#1


37  

Essentially, you want to generate a cartesian product and then concatenate the elements of each 2-tuple. This is easiest to do in query-syntax:

本质上,您希望生成一个笛卡尔积,然后将每个2元组的元素串联起来。这在查询语法中是最容易做到的:

var cartesianConcat = from a in seq1
                      from b in seq2
                      select a + b;

#2


59  

Use SelectMany when you want to form the Cartesian product of two lists:

当您想要形成两个列表的笛卡尔积时,请使用SelectMany:

aList.SelectMany(a => bList.Select(b => a + b))

船向一边倾斜的。SelectMany(= > bList。选择(b => a + b)

#3


25  

SelectMany is definitely the right approach, whether using multiple "from" clauses or with a direct call, but here's an alternative to Hightechrider's use of it:

SelectMany绝对是正确的方法,无论是使用多个“from”子句还是直接调用,但这里有一个替代Hightechrider使用它的方法:

var result = aList.SelectMany(a => bList, (a, b) => a + b);

I personally find this easier to understand as it's closer to the "multiple from" version: for each "a" in "aList", we produce a new sequence - in this case it's always "bList". For each (a, b) pair produced by the Cartesian join, we project to a result which is just the concatenation of the two.

我个人认为这更容易理解,因为它更接近“从”版本:对于“aList”中的每一个“a”,我们生成一个新的序列——在这种情况下,它总是“bList”。对于笛卡尔连接产生的每一个(a, b)对,我们将得到的结果就是这两个的串联。

Just to be clear: both approaches will work. I just prefer this one :)

明确一点:两种方法都可以。我只喜欢这个:

As to whether this is clearer than the query expression syntax... I'm not sure. I usually use method calls when it's just a case of using a single operator, but for Join, GroupJoin, SelectMany and GroupBy, query expressions do simplify things. Try both and see which you find more readable :)

至于这是否比查询表达式语法更清晰……我不确定。我通常在只使用一个操作符的情况下使用方法调用,但是对于Join、GroupJoin、SelectMany和GroupBy,查询表达式可以简化事情。试试这两种,看看哪一种更容易读懂:)

#1


37  

Essentially, you want to generate a cartesian product and then concatenate the elements of each 2-tuple. This is easiest to do in query-syntax:

本质上,您希望生成一个笛卡尔积,然后将每个2元组的元素串联起来。这在查询语法中是最容易做到的:

var cartesianConcat = from a in seq1
                      from b in seq2
                      select a + b;

#2


59  

Use SelectMany when you want to form the Cartesian product of two lists:

当您想要形成两个列表的笛卡尔积时,请使用SelectMany:

aList.SelectMany(a => bList.Select(b => a + b))

船向一边倾斜的。SelectMany(= > bList。选择(b => a + b)

#3


25  

SelectMany is definitely the right approach, whether using multiple "from" clauses or with a direct call, but here's an alternative to Hightechrider's use of it:

SelectMany绝对是正确的方法,无论是使用多个“from”子句还是直接调用,但这里有一个替代Hightechrider使用它的方法:

var result = aList.SelectMany(a => bList, (a, b) => a + b);

I personally find this easier to understand as it's closer to the "multiple from" version: for each "a" in "aList", we produce a new sequence - in this case it's always "bList". For each (a, b) pair produced by the Cartesian join, we project to a result which is just the concatenation of the two.

我个人认为这更容易理解,因为它更接近“从”版本:对于“aList”中的每一个“a”,我们生成一个新的序列——在这种情况下,它总是“bList”。对于笛卡尔连接产生的每一个(a, b)对,我们将得到的结果就是这两个的串联。

Just to be clear: both approaches will work. I just prefer this one :)

明确一点:两种方法都可以。我只喜欢这个:

As to whether this is clearer than the query expression syntax... I'm not sure. I usually use method calls when it's just a case of using a single operator, but for Join, GroupJoin, SelectMany and GroupBy, query expressions do simplify things. Try both and see which you find more readable :)

至于这是否比查询表达式语法更清晰……我不确定。我通常在只使用一个操作符的情况下使用方法调用,但是对于Join、GroupJoin、SelectMany和GroupBy,查询表达式可以简化事情。试试这两种,看看哪一种更容易读懂:)