What is the use of LINQ Zip operator

As per the MSDN Linq is

Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language.

LINQ provides a lot of operator out of the box. There is one operator, which is lesser-known. In this article, I will show how and where to use the LINQ Zip operator
Zip the operator is used when you want to iterates multiple collections at the same time.
Let’s consider the following example.

var firstNames = new string[] { "John", "Bill", "Tim", "Santosh" };
var lastNames = new String[] { "Doe", "Gates", "Sanders", "Singh" };

We have two collections of string, the first list have the firstName of the person and the second list have lastName of the person.
Now you want to create a collection which has the full name like John Doe,Bill Gates etc.
In this scenario, we can use the zip operator.

var fullName=firstNames.Zip(lastNames,(f,l)=> f +" "+l);

إرسال تعليق

Please do not post any spam link in the comment box😊

أحدث أقدم

Blog ads

CodeGuru