Zip Operator in LinQ C#.

Recently I found a very beautiful feature of LinQ that is “Zip Operator”. It merges two sequences by using the specified predicate function.

using System;
using System.Linq;
using System.Web.UI;

public partial class Zip_Operator : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ZipOperator();
        }
    }

    private void ZipOperator()
    {
        string[] country = { “China”, “India”, “Japan”, “South Korea”, “France” };
        string[] capital ={ “Beijing”,
                           “New Delhi”,
                           “Tokyo”,
                           “Seoul”,
                           “Paris”
                         };
        var countryCapital = country.Zip(capital, (countries, capitals) => countries + “: ” + capitals);
        string zip = “<table><th>Country : Capital</th>”;
        foreach (var str in countryCapital)
        {
            zip += “<tr><td>” + str.ToString() + “</td></tr>”;
        }
        zip += “</table>”;       
    }
}
Output is:

Country : Capital
China: Beijing
India: New Delhi
Japan: Tokyo
South Korea: Seoul
France: Paris
Advertisements

5 thoughts on “Zip Operator in LinQ C#.

  1. Just wish to say your article is as amazing. The clearness in your post
    is simply great and i could assume you’re an expert on this subject. Fine with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please carry on the enjoyable work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s