Monday, September 6, 2010

LINQ - Linq To Object Part 3 Group by

Linq to Object Part 2

As like sql we can group the objects using the group by operator in Linq.

Step 1
Create a Web Project.

Step 2
Create a Student class folder in app_code folder,it is look like this


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public class Student
{
    #region Property


    public String FirstName
    {
        get;
        set;
    }

    public String Country
    {
        get;
        set;
    }

    #endregion
}

Step 3
In default class [default.aspx.cs] write a method for store the student data which return Student list Object.it is look like this

 /// <summary>
    /// Add the in Student Object
    /// </summary>
    /// <returns>List</returns>
    private List<Student> AddData()
    {
        try
        {
            Student Std1 = new Student();
            Std1.FirstName = "Kishor";
            Std1.Country = "India";

            Student Std2 = new Student();
            Std2.FirstName = "Kakashi";
            Std2.Country = "Japan";

            Student Std3 = new Student();
            Std3.FirstName = "Lee";
            Std3.Country = "South Korea";

            Student Std4 = new Student();
            Std4.FirstName = "Ramdas";
            Std4.Country = "India";

            Student Std5 = new Student();
            Std5.FirstName = "John";
            Std5.Country = "USA";

            Student Std6 = new Student();
            Std6.FirstName = "Jiraiya";
            Std6.Country = "Japan";
            
            Student Std7 = new Student();
            Std7.FirstName = "Kedar";
            Std7.Country = "India";

            Student Std8 = new Student();
            Std8.FirstName = "Eun-ju";
            Std8.Country = "South Korea";

            Student Std9 = new Student();
            Std9.FirstName = "Naruto";
            Std9.Country = "Japan";

            Student Std10 = new Student();
            Std10.FirstName = "David";
            Std10.Country = "USA";

            Student Std11 = new Student();
            Std11.FirstName = "Shilpa";
            Std11.Country = "India";

            Student Std12 = new Student();
            Std12.FirstName = "Sakura";
            Std12.Country = "Japan";

            List<Student> StudentObj = new List<Student>();
            StudentObj.Add(Std1);
            StudentObj.Add(Std2);
            StudentObj.Add(Std3);
            StudentObj.Add(Std4);
            StudentObj.Add(Std5);
            StudentObj.Add(Std6);
            StudentObj.Add(Std7);
            StudentObj.Add(Std8);
            StudentObj.Add(Std9);
            StudentObj.Add(Std10);
            StudentObj.Add(Std11);
            StudentObj.Add(Std12);

            return StudentObj; 
  
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

Step 4
Group By
In default class [default.aspx.cs] write a method for to get the list of country name with count.it is look like this


 /// <summary>
    /// Group By - Get the list of country name with count
    /// </summary>
    /// <param name="ListObj">Specify List Object</param>
    /// <returns>Ilist</returns>
    private IList GroupBy(List<Student> ListObj)
    {
        try
        {
            var Query = from Q in ListObj
                        group Q by Q.Country into C
                        select new
                        {
                            Country = C.Key.Trim(),
                            Count = C.Count()  
                        };

            return Query.ToList();  
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

Step 5
Bind the data in gridview,it is look like this
 /// <summary>
    /// Bind The Data in gridview
    /// </summary>
    private void BindGridView()
    {
        try
        {
            GvStudent.DataSource = GroupBy(AddData());
            GvStudent.DataBind();  
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

Full Code

.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
            <asp:GridView ID="GvStudent" runat="server" CellPadding="4" ForeColor="#333333" 
                    GridLines="None">
                <RowStyle BackColor="#EFF3FB" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>
        
    
    </div>
    </form>
</body>
</html>

.aspx.cs [Code Behind]
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (IsPostBack == false)
            {
                BindGridView(); 
            }
        }
        catch (Exception)
        { }
    }

    #region Methods
    /// <summary>
    /// Add the in Student Object
    /// </summary>
    /// <returns>List</returns>
    private List<Student> AddData()
    {
        try
        {
            Student Std1 = new Student();
            Std1.FirstName = "Kishor";
            Std1.Country = "India";

            Student Std2 = new Student();
            Std2.FirstName = "Kakashi";
            Std2.Country = "Japan";

            Student Std3 = new Student();
            Std3.FirstName = "Lee";
            Std3.Country = "South Korea";

            Student Std4 = new Student();
            Std4.FirstName = "Ramdas";
            Std4.Country = "India";

            Student Std5 = new Student();
            Std5.FirstName = "John";
            Std5.Country = "USA";

            Student Std6 = new Student();
            Std6.FirstName = "Jiraiya";
            Std6.Country = "Japan";
            
            Student Std7 = new Student();
            Std7.FirstName = "Kedar";
            Std7.Country = "India";

            Student Std8 = new Student();
            Std8.FirstName = "Eun-ju";
            Std8.Country = "South Korea";

            Student Std9 = new Student();
            Std9.FirstName = "Naruto";
            Std9.Country = "Japan";

            Student Std10 = new Student();
            Std10.FirstName = "David";
            Std10.Country = "USA";

            Student Std11 = new Student();
            Std11.FirstName = "Shilpa";
            Std11.Country = "India";

            Student Std12 = new Student();
            Std12.FirstName = "Sakura";
            Std12.Country = "Japan";

            List<Student> StudentObj = new List<Student>();
            StudentObj.Add(Std1);
            StudentObj.Add(Std2);
            StudentObj.Add(Std3);
            StudentObj.Add(Std4);
            StudentObj.Add(Std5);
            StudentObj.Add(Std6);
            StudentObj.Add(Std7);
            StudentObj.Add(Std8);
            StudentObj.Add(Std9);
            StudentObj.Add(Std10);
            StudentObj.Add(Std11);
            StudentObj.Add(Std12);

            return StudentObj; 
  
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

    /// <summary>
    /// Group By - Get the list of country name with count
    /// </summary>
    /// <param name="ListObj">Specify List Object</param>
    /// <returns>Ilist</returns>
    private IList GroupBy(List<Student> ListObj)
    {
        try
        {
            var Query = from Q in ListObj
                        group Q by Q.Country into C
                        select new
                        {
                            Country = C.Key.Trim(),
                            Count = C.Count()  
                        };

            return Query.ToList();  
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }

    /// <summary>
    /// Bind The Data in gridview
    /// </summary>
    private void BindGridView()
    {
        try
        {
            GvStudent.DataSource = GroupBy(AddData());
            GvStudent.DataBind();  
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);  
        }
    }
    #endregion
}

Run the Project

Download
Download Source Code

No comments:

Post a Comment