Enumerable.Zip Método
Definición
Sobrecargas
| Zip<TFirst,TSecond,TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>) |
Aplica la función especificada a los elementos correspondientes de dos secuencias, lo que genera una secuencia de resultados.Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results. |
| Zip<TFirst,TSecond>(IEnumerable<TFirst>, IEnumerable<TSecond>) |
Genera una secuencia de tuplas con elementos a partir de las dos secuencias especificadas.Produces a sequence of tuples with elements from the two specified sequences. |
Zip<TFirst,TSecond,TResult>(IEnumerable<TFirst>, IEnumerable<TSecond>, Func<TFirst,TSecond,TResult>)
Aplica la función especificada a los elementos correspondientes de dos secuencias, lo que genera una secuencia de resultados.Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
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)
Parámetros de tipo
- TFirst
Tipo de los elementos de la primera secuencia de entrada.The type of the elements of the first input sequence.
- TSecond
Tipo de los elementos de la segunda secuencia de entrada.The type of the elements of the second input sequence.
- TResult
Tipo de los elementos de la secuencia de resultados.The type of the elements of the result sequence.
Parámetros
- first
- IEnumerable<TFirst>
Primera secuencia que se va a combinar.The first sequence to merge.
- second
- IEnumerable<TSecond>
Segunda secuencia que se va a combinar.The second sequence to merge.
- resultSelector
- Func<TFirst,TSecond,TResult>
Función que especifica cómo combinar los elementos de las dos secuencias.A function that specifies how to merge the elements from the two sequences.
Devoluciones
- IEnumerable<TResult>
IEnumerable<T> que contiene elementos combinados de las dos secuencias de entrada.An IEnumerable<T> that contains merged elements of two input sequences.
Excepciones
first o second es null.first or second is null.
Ejemplos
En el ejemplo de código siguiente se muestra cómo utilizar el Zip método para combinar dos secuencias.The following code example demonstrates how to use the Zip method to merge two sequences.
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
Comentarios
Este método se implementa mediante la ejecución aplazada.This method is implemented by using deferred execution. El valor devuelto inmediato es un objeto que almacena toda la información necesaria para realizar la acción.The immediate return value is an object that stores all the information that is required to perform the action. La consulta representada por este método no se ejecuta hasta que el objeto se enumera mediante una llamada a su GetEnumerator método directamente o mediante foreach en Visual C# o For Each en Visual Basic.The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.
El método combina cada elemento de la primera secuencia con un elemento que tiene el mismo índice en la segunda secuencia.The method merges each element of the first sequence with an element that has the same index in the second sequence. Si las secuencias no tienen el mismo número de elementos, el método combina las secuencias hasta que llega al final de una de ellas.If the sequences do not have the same number of elements, the method merges sequences until it reaches the end of one of them. Por ejemplo, si una secuencia tiene tres elementos y la otra tiene cuatro, la secuencia de resultados solo tendrá tres elementos.For example, if one sequence has three elements and the other one has four, the result sequence will have only three elements.
Se aplica a
Zip<TFirst,TSecond>(IEnumerable<TFirst>, IEnumerable<TSecond>)
Genera una secuencia de tuplas con elementos a partir de las dos secuencias especificadas.Produces a sequence of tuples with elements from the two specified sequences.
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,TSecond)> 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))
Parámetros de tipo
- TFirst
Tipo de los elementos de la primera secuencia de entrada.The type of the elements of the first input sequence.
- TSecond
Tipo de los elementos de la segunda secuencia de entrada.The type of the elements of the second input sequence.
Parámetros
- first
- IEnumerable<TFirst>
Primera secuencia que se va a combinar.The first sequence to merge.
- second
- IEnumerable<TSecond>
Segunda secuencia que se va a combinar.The second sequence to merge.
Devoluciones
- IEnumerable<ValueTuple<TFirst,TSecond>>
Secuencia de tuplas con elementos tomados de la primera y la segunda secuencia, en ese orden.A sequence of tuples with elements taken from the first and second sequences, in that order.