How to use projection operators with Linq

By
The projection operator is a very important concepts used in LINQ. The SelectMany() and Select () function are the type of projection operator which are used to define a projection to select values of the result based on a selector function (Ex. split()). In this tutorial i have used split() function to split the words from given strings. Here i have used select () clause for this purpose.You can use the SelectMany() clause to display the data.In this, i have  splitted the words from the strings where i  found the blank space in the sentence.  
There are some steps to implement this concepts as given below:-
Step 1 :- First open your visual studio --> File --> New --> Website -->Select ASP.NET Empty Website--> OK --> Open Solution Explorer -->Add a New web Form (Default.aspx)--> Drag and Drop ListBox and Button control  on the page from toolbox as given below:-

webform

Step 2 :- Now write the C# codes on Submit Button clicks (Default.aspx.cs) as given below:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string[] str = new string[] { "This is msdotnet.co.in", "You can learn lot of things from here", "Now days .NET is more important in development" };
        IEnumerable values = str.Select(s => s.Split(' '));
        foreach (string[] data in values)
        {
            foreach (string value1 in data)
            {
                ListBox1.Items.Add(value1);
        }
}
    }
}


Step 3 :- Now Run the application (press F5)--> You will see the following output as shown below:-
output

Note :- You can use this concepts in your real life projects.

You can run this whole application on your system after download it from below.
Please share this application and help to other students also. 
Download Attached File
 Download

0 comments:

Post a Comment

Powered by Blogger.