如何将两个或多个字符串列表中的每个字符串组合在一起?

时间:2017-04-12 17:45:33

标签: c# linq

我想要做的是说我有一个列表列表的列表......

list1 = "A", "B", "C"
list2 = "1", "2", "3"
list3 = "Dog"
...

然后我得到一个带

的列表
  newList = "A1Dog", "A2Dog", "A3Dog", "B1Dog", "B2Dog", "B3Dog", "C1Dog", 
  "C2Dog", "C3Dog" 

有没有办法用LINQ做这个或者有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

是的,您可以简单地使用多个from子句:

var result =
    from x in list1
    from y in list2
    from z in list3
    select x+y+z;

csharp交互式shell中,这给出了:

csharp> var list1 = new string[] {"A", "B", "C"};
csharp> var list2 = new string[] {"1", "2", "3"}; 
csharp> var list3 = new string[] {"Dog"};         
csharp>     var result =
      >         from x in list1
      >         from y in list2
      >         from z in list3
      >         select x+y+z;
csharp> result
{ "A1Dog", "A2Dog", "A3Dog", "B1Dog", "B2Dog", "B3Dog", "C1Dog", "C2Dog", "C3Dog" }