Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c语言中不同列表中所有组合的串联_C#_Combinatorics - Fatal编程技术网

C# c语言中不同列表中所有组合的串联

C# c语言中不同列表中所有组合的串联,c#,combinatorics,C#,Combinatorics,我有一个列表,我把它分成不同的列表 发件人: 进入 如何从该列表中找到所有可能的组合,输出如下: a,it,cat b,it,cat c,it,cat a,am,cat b,am,cat c,am,cat . . . . etc so on... 只需以嵌套方式循环遍历每个列表并组合: StringBuilder sb = new StringBuilder(); for(int i =0; i < list1.Length; i++){ for(int j =0; j < l

我有一个列表,我把它分成不同的列表

发件人:

进入

如何从该列表中找到所有可能的组合,输出如下:

a,it,cat b,it,cat c,it,cat a,am,cat b,am,cat c,am,cat . . . . etc so on...
只需以嵌套方式循环遍历每个列表并组合:

StringBuilder sb = new StringBuilder();

for(int i =0; i < list1.Length; i++){
  for(int j =0; j < list2.Length; j++){
    for(int x =0; x < list3.Length; x++){
       sb.AppendFormat("{0},{1},{2}\n", list1[i], list2[j], list3[x]);
    }
  }
}

string result = sb.ToString();

只需以嵌套方式循环遍历每个列表并组合:

StringBuilder sb = new StringBuilder();

for(int i =0; i < list1.Length; i++){
  for(int j =0; j < list2.Length; j++){
    for(int x =0; x < list3.Length; x++){
       sb.AppendFormat("{0},{1},{2}\n", list1[i], list2[j], list3[x]);
    }
  }
}

string result = sb.ToString();
怎么样

List<string> l1 = new List<string>();
List<string> l2 = new List<string>();
List<string> l3 = new List<string>();
l1.Add("1");
l1.Add("2");
l1.Add("3");
l2.Add("a");
l2.Add("b");
l2.Add("c");
l3.Add(".");
l3.Add("!");
l3.Add("@");

var product = from a in l1
from b in l2
from c in l3
select  a+","+b+","+c;
怎么样

List<string> l1 = new List<string>();
List<string> l2 = new List<string>();
List<string> l3 = new List<string>();
l1.Add("1");
l1.Add("2");
l1.Add("3");
l2.Add("a");
l2.Add("b");
l2.Add("c");
l3.Add(".");
l3.Add("!");
l3.Add("@");

var product = from a in l1
from b in l2
from c in l3
select  a+","+b+","+c;
当然,您可以使用ToList直接将其转换为列表,这样就根本不需要foreach了。无论你需要对结果做什么

当然,您可以使用ToList直接将其转换为列表,这样就根本不需要foreach了。无论你需要对结果做什么

List<string> result = new List<string>();
foreach (var item in list1
    .SelectMany(x1 => list2
        .SelectMany(x2 => list3
            .Select(x3 => new { X1 = x1, X2 = x2, X3 = x3 }))))
{
    result.Add(string.Format("{0}, {1}, {2}", item.X1, item.X2, item.X3));
}