Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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#将两个列表合并为一个列表,如Enumerable.ConvertAll_C#_List_Merge_Convertall - Fatal编程技术网

C#将两个列表合并为一个列表,如Enumerable.ConvertAll

C#将两个列表合并为一个列表,如Enumerable.ConvertAll,c#,list,merge,convertall,C#,List,Merge,Convertall,我想将具有相同索引的两个列表转换为一个列表。 鉴于此: List<double> ListA = new List<double> { 1, 2, 3, 4, 5 }; List<string> ListB = new List<string> { "A","B","C","D","E" }; List ListA=新列表{1,2,3,4,5}; List ListB=新列表{“A”、“B”、“C”、“D”、“E”}; 到目前为止,

我想将具有相同索引的两个列表转换为一个列表。 鉴于此:

List<double> ListA = new List<double> {  1,  2,  3,  4,  5  };
List<string> ListB = new List<string> { "A","B","C","D","E" };
List ListA=新列表{1,2,3,4,5};
List ListB=新列表{“A”、“B”、“C”、“D”、“E”};
到目前为止,我使用的是:

List<string> UNHAPPY = new List<string>();
for (int ListIndex = 0; ListIndex < ListA.Count; ListIndex++)
{
    UNHAPPY.Add(ListB[ListIndex]+ListA[ListIndex].ToString());
}
// such that UNHAPPY == new List<string> {"A1", "B2", "C3", "D4", "E5"}
List=newlist();
对于(int ListIndex=0;ListIndex
但我确实希望尽可能使用简短的代码,如下所示(类似于Enumerable.ConvertAll):

List HAPPY=SOME_容器(ListA,ListB)。SOME_选择器((a,b)=>b+a.ToString());
//这样HAPPY==新列表{“A1”、“B2”、“C3”、“D4”、“E5”}

有什么快速的方法吗?提前谢谢你

您正在寻找
Enumerable.Zip

first.Zip(second, (f, s) => f + s.ToString());

LINQ对此有一个方法,称为:

它接受两个序列和一个
Func
委托,并将委托应用于来自这两个序列的项目对

注意:
a
调用
ToString
是多余的,C#将通过对相应对象调用
ToString
将字符串连接到任何对象,包括
int
。我更喜欢字符串插值,尤其是当您需要连接两个以上的项时。

您可以通过以下方法来实现:

这将产生:

csharp> List<double> ListA = new List<double> {  1,  2,  3,  4,  5  };
csharp> List<string> ListB = new List<string> { "A","B","C","D","E" };
csharp> ListA.Zip(ListB, (a,b) => b + a.ToString());
{ "A1", "B2", "C3", "D4", "E5" }
csharp>List ListA=新列表{1,2,3,4,5};
csharp>List ListB=新列表{“A”、“B”、“C”、“D”、“E”};
csharp>ListA.Zip(ListB,(a,b)=>b+a.ToString();
{“A1”、“B2”、“C3”、“D4”、“E5”}
Zip
i同时对两个iTerable进行重新编译,并生成由函数构造的产品。从一个iterables用完的那一刻起,
Zip
就停止了

var res = ListA.Zip(ListB, (a,b) => $"{b}{a}");
ListA.Zip(ListB, (a,b) => b + a.ToString());
csharp> List<double> ListA = new List<double> {  1,  2,  3,  4,  5  };
csharp> List<string> ListB = new List<string> { "A","B","C","D","E" };
csharp> ListA.Zip(ListB, (a,b) => b + a.ToString());
{ "A1", "B2", "C3", "D4", "E5" }