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>)

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

public:
generic <typename TFirst, typename TSecond, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TResult> ^ Zip(System::Collections::Generic::IEnumerable<TFirst> ^ first, System::Collections::Generic::IEnumerable<TSecond> ^ second, Func<TFirst, TSecond, TResult> ^ resultSelector);
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);
static member Zip : seq<'First> * seq<'Second> * Func<'First, 'Second, 'Result> -> seq<'Result>
<Extension()>
Public Function Zip(Of TFirst, TSecond, TResult) (first As IEnumerable(Of TFirst), second As IEnumerable(Of TSecond), resultSelector As Func(Of TFirst, TSecond, TResult)) As IEnumerable(Of TResult)

类型参数

TFirst

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

TSecond

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

TResult

结果序列的元素的类型。

参数

first
IEnumerable<TFirst>

要合并的第一个序列。

second
IEnumerable<TSecond>

要合并的第二个序列。

resultSelector
Func<TFirst,TSecond,TResult>

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

返回

IEnumerable<TResult>

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

例外

firstsecondnull

示例

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

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
Dim numbers() As Integer = {1, 2, 3, 4}
Dim words() As String = {"one", "two", "three"}
Dim numbersAndWords = numbers.Zip(words, Function(first, second) first & " " & second)

For Each item In numbersAndWords
    Console.WriteLine(item)
Next

' This code produces the following output:

' 1 one
' 2 two
' 3 three

注解

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

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

另请参阅

适用于

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

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

public:
generic <typename TFirst, typename TSecond, typename TThird>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<ValueTuple<TFirst, TSecond, TThird>> ^ Zip(System::Collections::Generic::IEnumerable<TFirst> ^ first, System::Collections::Generic::IEnumerable<TSecond> ^ second, System::Collections::Generic::IEnumerable<TThird> ^ third);
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);
static member Zip : seq<'First> * seq<'Second> * seq<'hird> -> seq<ValueTuple<'First, 'Second, 'hird>>
<Extension()>
Public Function Zip(Of TFirst, TSecond, TThird) (first As IEnumerable(Of TFirst), second As IEnumerable(Of TSecond), third As IEnumerable(Of TThird)) As IEnumerable(Of ValueTuple(Of TFirst, TSecond, TThird))

类型参数

TFirst

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

TSecond

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

TThird

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

参数

first
IEnumerable<TFirst>

要合并的第一个序列。

second
IEnumerable<TSecond>

要合并的第二个序列。

third
IEnumerable<TThird>

要合并的第三个序列。

返回

IEnumerable<ValueTuple<TFirst,TSecond,TThird>>

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

另请参阅

适用于

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

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

public:
generic <typename TFirst, typename TSecond>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<ValueTuple<TFirst, TSecond>> ^ Zip(System::Collections::Generic::IEnumerable<TFirst> ^ first, System::Collections::Generic::IEnumerable<TSecond> ^ second);
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);
static member Zip : seq<'First> * seq<'Second> -> seq<ValueTuple<'First, 'Second>>
<Extension()>
Public Function Zip(Of TFirst, TSecond) (first As IEnumerable(Of TFirst), second As IEnumerable(Of TSecond)) As IEnumerable(Of ValueTuple(Of TFirst, TSecond))

类型参数

TFirst

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

TSecond

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

参数

first
IEnumerable<TFirst>

要合并的第一个序列。

second
IEnumerable<TSecond>

要合并的第二个序列。

返回

IEnumerable<ValueTuple<TFirst,TSecond>>

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

另请参阅

适用于