C# Linq Zip Method


Merge sequences using predicate with Zip method.

Here are our arrays to be merged.

int[] intArray = { 10, 20, 30, 40 };
string[] stringArray = { "Jack", "Tim", "Henry", "Tom" };

Now let’s use the Zip method to merge both the arrays.

intArray.AsQueryable().Zip(stringArray, (one, two) => one + " " + two)

The following is our code.

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      int[] intArray = { 10, 20, 30, 40 };
      string[] stringArray = { "Jack", "Tim", "Henry", "Tom" };
      var mergedSeq = intArray.AsQueryable().Zip(stringArray, (one, two) => one + " " + two);
      foreach (var ele in mergedSeq)
      Console.WriteLine(ele);
   }
}

Output

10 Jack
20 Tim
30 Henry
40 Tom

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

910 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements