How to Use Grouping and Sorting operators Concepts in LINQ Query

By
Hi Friends ! Today , i am going to show you "How to use Grouping and Sorting operators Concepts in LINQ Query ".Here i will show you Group query results based on some key values. You can easily sort the Integer arrays values in two order :-
  1. Ascending
  2. Descending
You can use this two concepts(Grouping and Sorting) in your asp.net web applications.Now days this concepts is mostly used in our web applications.You can easily modify it, based on your requirements.
There are some steps to implement this concepts in your asp.net web application 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 List Box and Button controls on the page from toolbox as shown below:-

webform

Step 2 :- Now open Default.aspx.cs page (Each button's clicks ) -->Write the c# codes as given below:- 
using System;
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 = { "123","223","Ram","ajeet","Ramesh","pawan","monika",                    "sandeep","priya","2897","pankaj","aman","lokesh","anish","karan",".999","neha"};
        var group_value = from gv in str group gv by gv[0] into values
        select new {initial_value=values.Key,str=values};
        foreach (var values in group_value)
        {
            ListBox1.Items.Add("Values that start with:- " + values.initial_value.ToString());
            foreach (var i in values.str)
                ListBox1.Items.Add(i);

        }

    }
    int[] values = { 23, 56, 67, 20, 14, 13, 30, 45, 67, 85, 25, 1, 8, 90, 100, 400, 53, 456, 500, 345, 370, 430 };
    protected void Button2_Click(object sender, EventArgs e)
    {
        ListBox2.Items.Add("Ascending order values are:-");   
        var sort_values = from num in values
                         orderby num ascending
                         select num;
        foreach (int m in sort_values)
        {
            ListBox2.Items.Add(m.ToString());
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        ListBox3.Items.Add("Decending order values are:-");
        var sort_items = from n in values
                         orderby n descending
                         select n;
        foreach (int p in sort_items)
        {
            ListBox3.Items.Add(p.ToString());
        }
    }
}

Step 3 :- Now Run the Application (press F5)-->and press Set_Grouping ,Ascending and Descending buttons respectively --> You will see the following output as shown below:-



Note :- You can  also apply these operators in database's values for grouping and sorting the values.

I hope this helpful for you.
 Please share this post with your friends also.
 Download Attached file
  Download

0 comments:

Post a comment

Powered by Blogger.