// a and b are corresponding items from listA and listB
foreach (var (a, b) in listA.Zip(listB, (a, b) => (a, b))) {
//TODO: relevant code here; a is from listA, b is from listB
}
例如:
List<int> listA = new List<int>() {1, 2, 3};
List<string> listB = new List<string>() {"A", "B", "C"};
foreach (var (a, b) in listA.Zip(listB, (a, b) => (a, b)))
{
Console.WriteLine($"{a} : {b}");
}
3条答案
按热度按时间syqv5f0l1#
您是否正在寻找Zip?如果您要枚举配对(listA中的第一个项目,listB中的第一个项目;列表A中的第2个项目,列表B中的第2个项目...列表A中的第n个项目,列表B中的第n个项目):
例如:
输出:
求你了fiddle yourself
qyzbxkaa2#
我会这样做:
我知道它不是LINQ,但我认为这种方法更简单
dphi5xsq3#
您可以
Select
项目沿着索引,如下所示:然后,您可以应用
First
,通过index
值将它们连接起来:我手边没有.NET环境,我也很长一段时间没有在里面工作了,所以如果有错别字,让我知道。