Share Blog

Tuesday, May 27, 2014

LINQ TO SQL:Take ,Skip,Max,Min,Count,Average,First Operator



--------------------------CREATE A TABLE---------------------------
create table Operator(Id int primary key, Name varchar(50),Country varchar(50),City varchar(50),Salary float)
select * from Operator
--------------INSERT DATA IN TABLE--------------------
insert into Operator values(1,'Mukesh', 'India','Sikar',5000),(2,'Mohit', 'SriLanka','colmbo',8000),
(3,'Naveen', 'Pakistan','Panjab',9000),(4,'Shivam', 'India','Jaipur',15000),(5,'Bichitra', 'India','Noida',10000

1. Create a Data Context Class

I create a data context class that has tables or a stored procedure. One figure shows the Operator table in a data context class and another all employee data in the database. This DATACLASS object has a relationship with the Operator table.







------Default.aspx.cs Code--------
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
{
    DataClassesDataContext dc = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        var v = (from emp in dc.Operators
                        select emp).Take(2);

       GridView1.DataSource = v;
       GridView1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        var v = (from emp in dc.Operators
                        select emp).Skip(3);

        GridView1.DataSource = v;
        GridView1.DataBind();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        var count = (from i in dc.Operators select i).Count();
        Response.Write(count);
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        var maxsalary = (from i in dc.Operators select i.Salary).Max();
        Response.Write(maxsalary);
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        var min = (from i in dc.Operators select i.Salary).Min();
        Response.Write(min);
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        var avg = (from i in dc.Operators select i.Salary).Average();
        Response.Write("<script>alert('"+avg+"')</script>");
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        var first = (from i in dc.Operators select i.Salary).First();
        Response.Write("<script>alert('" + first + "')</script>");
        //GridView1.Visible = false;
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

}

No comments:

Post a Comment