Linq Zip Method

Linq Zip Method with Example

In this article, I am going to discuss the LINQ Zip Method with an example. Please read our previous article before proceeding to this article where we discussed the LINQ Prepend Method with an example.

Zip Method:

The Linq Zip method is used to apply a specified function to the corresponding elements of two sequences and producing a sequence of the results. The signature of this method is given below.

Linq Zip Method Signature

Type Parameters:
  1. TFirst: The type of the elements of the first input sequence.
  2. TSecond: The type of the elements of the second input sequence.
  3. TResult: The type of the elements of the result sequence.
Parameters:
  1. IEnumerable<TFirst> first: The first sequence to merge.
  2. IEnumerable<TSecond> second: The second sequence to merge.
  3. Func<TFirst,TSecond,TResult> resultSelector: A function that specifies how to merge the elements from the two sequences.

Returns: This method is going to return an IEnumerable<T> that contains merged elements of two input sequences.

Exceptions: This method is going to throw ArgumentNullException when either the first or the second input sequence is null.

Note: The Zip method merges each element of the first sequence with an element in the second sequence that has the same index position. If both the sequences do not have the same number of elements, then the Zip method merges sequences until it reaches the end the sequence which contains less number of elements. For example, if one sequence has five elements and the other sequence has four elements, then the result sequence will have only four elements.

Example:

The following example shows how to merge two sequences using the Linq Zip method.

using System;
using System.Linq;

namespace ThreadingDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbersSequence = { 10, 20, 30, 40, 50 };
            string[] wordsSequence = { "Ten", "Twenty", "Thirty", "Fourty" };

            var resultSequence = numbersSequence.Zip(wordsSequence, (first, second) => first + " - " + second);

            foreach (var item in resultSequence)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
}

Output:

ZIP Method Output in Linq

The first sequence contains 5 elements whereas the second sequence contains 4 elements. So, for the fifth element of the first sequence, there is no corresponding fifth element in the second sequence. As a result, the Zip method merges the four elements and that’s what we have seen in the output.

Note:

The Zip method is implemented by using the deferred execution. So, the immediate return value of this method is going to be an object which stores all the required information which is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using for each loop.

That’s it for today. I hope you understood the Linq ZIP Method. In the next article, I am going to discuss the Deferred Execution VS Immediate Execution in LINQ with some examples.

Leave a Reply

Your email address will not be published. Required fields are marked *