Enumerable.Zip<TFirst, TSecond, TResult> Method
Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
Assembly: System.Core (in System.Core.dll)
public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>( this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector )
Type Parameters
- TFirst
The type of the elements of the first input sequence.
- TSecond
The type of the elements of the second input sequence.
- TResult
The type of the elements of the result sequence.
Parameters
- first
- Type: System.Collections.Generic.IEnumerable<TFirst>
The first input sequence.
- second
- Type: System.Collections.Generic.IEnumerable<TSecond>
The second input sequence.
- resultSelector
- Type: System.Func<TFirst, TSecond, TResult>
A function that specifies how to combine the corresponding elements of the two sequences.
Return Value
Type: System.Collections.Generic.IEnumerable<TResult>An IEnumerable<T> that contains elements of the two input sequences, combined by resultSelector.
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable<TFirst>. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).Exception | Condition |
---|---|
ArgumentNullException | first or second is null. |
The method steps through the two input sequences, applying function resultSelector to corresponding elements of the two sequences. The method returns a sequence of the values that are returned by resultSelector. If the input sequences do not have the same number of elements, the method combines elements until it reaches the end of one of the sequences. For example, if one sequence has three elements and the other one has four, the result sequence has only three elements.
This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query that this method represents 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.
The following code example demonstrates how to use the Zip<TFirst, TSecond, TResult> method.
int[] numbers = { 1, 2, 3, 4 }; string[] words = { "one", "two", "three" }; // The following example concatenates corresponding elements of the // two input sequences. var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second); foreach (var item in numbersAndWords) Console.WriteLine(item); Console.WriteLine(); // The following example returns the larger element from each // corresponding pair in the input sequences. int[] numbers2 = { 5, 2, 1, 3, 6 }; var largerElements = numbers2.Zip(numbers, (first, second) => first > second ? first : second); foreach (var item in largerElements) Console.WriteLine(item); Console.WriteLine(); // The following example calculates the commission that is earned by // an employee who had sales in the first three quarters of the year. double[] quarterlySales = { 4023.52, 7701.65, 2435.20 }; double[] quarterlyRate = { 0.25, 0.2, 0.3, 0.2 }; var totalCommission = quarterlySales.Zip(quarterlyRate, (first, second) => first * second).Sum(); Console.WriteLine(totalCommission); // Output: // 1 one // 2 two // 3 three // 5 // 2 // 3 // 4 // 3276.77
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.