text: Back to: LINQ Tutorial For Beginners and Professionals
  Linq Zip in C# with Example
  In this article, I am going to discuss the LINQ Zip in C# with examples. Please read our previous article before proceeding to this article where we discussed the LINQ Prepend Method with an example. At the end of this article, you will understand what is Linq Zip method and the need and use of C# Linq Zip Method with examples.
  Linq Zip Method:
  The Linq Zip Method in C# 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.
  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:
  - IEnumerable<TFirst> first: The first sequence to merge.
  - IEnumerable<TSecond> second: The second sequence to merge.
  - 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 of the sequence which contains fewer 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:
  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 in C#. In the next article, I am going to discuss the Deferred Execution VS Immediate Execution in LINQ with some examples.