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

來源:
Zip.cs
來源:
Zip.cs
來源:
Zip.cs

將指定的函式套用至兩個序列的對應項目,產生結果的序列。

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# 或 Visual Basic 中使用 foreach 來列舉物件,否則 For Each 不會執行這個方法所表示的查詢。

方法會將第一個序列的每個元素與在第二個序列中具有相同索引的專案合併。 如果序列沒有相同數目的專案,方法會定序序列,直到到達其中一個序列的結尾為止。 例如,如果一個序列有三個元素,另一個序列有四個元素,則結果序列只會有三個元素。

另請參閱

適用於

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

來源:
Zip.cs
來源:
Zip.cs
來源:
Zip.cs

產生具有來自三個指定序列之元素的元組序列。

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

來源:
Zip.cs
來源:
Zip.cs
來源:
Zip.cs

從兩個指定序列中的元素產生一系列元組。

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

取自第一個和第二個序列元素的一系列元組,以該順序排序。

另請參閱

適用於