Enumerable.Zip 方法

定义

重载

Zip<TFirst,TSecond,TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>)

将指定函数应用于两个序列的对应元素,以生成结果序列。

Zip<TFirst,TSecond,TThird>(IEnumerable<TFirst>, IEnumerable<TSecond>, IEnumerable<TThird>)

使用三个指定序列中的元素生成元组序列。

Zip<TFirst,TSecond>(IEnumerable<TFirst>, IEnumerable<TSecond>)

使用两个指定序列中的元素生成元组序列。

Zip<TFirst,TSecond,TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>)

Source:
Zip.cs
Source:
Zip.cs
Source:
Zip.cs

将指定函数应用于两个序列的对应元素,以生成结果序列。

C#
public static System.Collections.Generic.IEnumerable<TResult> Zip<TFirst,TSecond,TResult> (this System.Collections.Generic.IEnumerable<TFirst> first, System.Collections.Generic.IEnumerable<TSecond> second, Func<TFirst,TSecond,TResult> resultSelector);

类型参数

TFirst

第一个输入序列中的元素的类型。

TSecond

第二个输入序列中的元素的类型。

TResult

结果序列的元素的类型。

参数

first
IEnumerable<TFirst>

要合并的第一个序列。

second
IEnumerable<TSecond>

要合并的第二个序列。

resultSelector
Func<TFirst,TSecond,TResult>

用于指定如何合并这两个序列中的元素的函数。

返回

IEnumerable<TResult>

一个包含两个输入序列中的合并元素的 IEnumerable<T>

例外

firstsecondnull

示例

下面的代码示例演示如何使用 Zip 方法合并两个序列。

C#
int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };

var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);

foreach (var item in numbersAndWords)
    Console.WriteLine(item);

// This code produces the following output:

// 1 one
// 2 two
// 3 three

注解

此方法通过使用延迟执行来实现。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 在通过直接调用GetEnumerator其方法或在 C# For Eachforeach Visual Basic 中使用 来枚举对象之前,不会执行此方法表示的查询。

方法将第一个序列的每个元素与在第二个序列中具有相同索引的元素合并。 如果序列的元素数不同,则 方法会合并序列,直到到达其中一个序列的末尾。 例如,如果一个序列有三个元素,另一个序列有四个元素,则结果序列将只有三个元素。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Zip<TFirst,TSecond,TThird>(IEnumerable<TFirst>, IEnumerable<TSecond>, IEnumerable<TThird>)

Source:
Zip.cs
Source:
Zip.cs
Source:
Zip.cs

使用三个指定序列中的元素生成元组序列。

C#
public static System.Collections.Generic.IEnumerable<(TFirst First, TSecond Second, TThird Third)> Zip<TFirst,TSecond,TThird> (this System.Collections.Generic.IEnumerable<TFirst> first, System.Collections.Generic.IEnumerable<TSecond> second, System.Collections.Generic.IEnumerable<TThird> third);

类型参数

TFirst

第一个输入序列中的元素的类型。

TSecond

第二个输入序列中的元素的类型。

TThird

第三个输入序列的元素的类型。

参数

first
IEnumerable<TFirst>

要合并的第一个序列。

second
IEnumerable<TSecond>

要合并的第二个序列。

third
IEnumerable<TThird>

要合并的第三个序列。

返回

IEnumerable<ValueTuple<TFirst,TSecond,TThird>>

元组的序列,其元素取自第一个、第二个和第三个序列,按该顺序。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

Zip<TFirst,TSecond>(IEnumerable<TFirst>, IEnumerable<TSecond>)

Source:
Zip.cs
Source:
Zip.cs
Source:
Zip.cs

使用两个指定序列中的元素生成元组序列。

C#
public static System.Collections.Generic.IEnumerable<(TFirst First, TSecond Second)> Zip<TFirst,TSecond> (this System.Collections.Generic.IEnumerable<TFirst> first, System.Collections.Generic.IEnumerable<TSecond> second);

类型参数

TFirst

第一个输入序列中的元素的类型。

TSecond

第二个输入序列中的元素的类型。

参数

first
IEnumerable<TFirst>

要合并的第一个序列。

second
IEnumerable<TSecond>

要合并的第二个序列。

返回

IEnumerable<ValueTuple<TFirst,TSecond>>

一组元组序列,其中的元素按该顺序取自第一个和第二个序列。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9