+92 332 4229 857 99ProjectIdeas@Gmail.com

LINQ Zip() Method (C#.net)



LINQ Zip() Method (C#.net):
The following code shows you how to add elements of two arrays parallel. for example firstArray{2,3,4,1,5} and secodArray {1, 2 ,3 , 4 , 5} then after LINQ Zip() method the output would be {3,5,7,5,10}. Although you can do through loop but it is lengthy method. Here is the code below...

Code


int[] arr1 = { 2, 1, 2, 3, 7, 5, 10, 9 };

int[] arr2 = { 3, 5, 7, 2, 7, 5, 11, 19 };

var query = arr1.Zip(arr2, (a, b) => (a + b));

       foreach (var item in query)
            Console.Write(item + " ");

Output of the program is:
5 6 9 5 14 10 21 28 



Concatenates two arrays parallel Through Zip() method.

int[] numb = { 1, 2, 3, 4, 5 };

string[] words = { "one", "two", "three", "four", "five" };

var query = numb.Zip(words, (a,b) => (a + " " + b));

    foreach (var item in query)
         Console.WriteLine(item + " ");



Output of the program is:
1 one
2 two
3 three
4 four
5 five 

0 comments: