Reader Level:
ARTICLE

Take and Skip Operator In LINQ to SQL

Posted by Sandeep Singh Shekhawat Articles | LINQ December 06, 2012
In this article I am going to explain the Take and Skip operators in LINQ to SQL. The Take operator is used to return a given number of rows from a database table and the Skip operator skips over a specifed number of rows in a database table.
  • 0
  • 0
  • 17043

In this article I am going to explain the Take and Skip operators in LINQ to SQL. The Take operator is used to return a given number of rows from a database table and the Skip operator skips over a specifed number of rows in a database table.

1. Create a Data Context Class

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

Image1.jpg

2. Create UI Design

I use a GridView to show employee data. This GridView code is:

<
asp:GridView ID="gridEmployee" runat="server" AutoGenerateColumns="false">
</
asp:GridView
>


3. Take Operator

The Take operator returns a specified number of contiguous rows from the starting point of the database table. The Take operator specifies how many rows we want from the start position of the table but when we define a criteria in that case this criteria is evaluated first before the start position is determined.
It works similar to the SQL TOP keyword where we get a specific number of contiguous top rows from a database table or can get top rows according to certain criteria.

The following code gets the top 5 rows from an EMPLOYEE object. It shows the top 5 rows from the EMPLOYEE table and shows them in the GridView.

private void GetEmployee()

    {

EmployeeOperationDataContext employeeContext = new EmployeeOperationDataContext();

 

        var employee = (from emp in employeeContext.EMPLOYEEs                       

                        select emp).Take(5);

 

        gridEmployee.DataSource = employee;

        gridEmployee.DataBind();

    }

The following code gets the top 5 rows from an EMPLOYEE object. These employees have a salary greater than 1200. They are shown in a GridView.

Image2.jpg

private void GetEmployee()

    {

        EmployeeOperationDataContext employeeContext = new EmployeeOperationDataContext();

 

        var employee = (from emp in employeeContext.EMPLOYEEs

                        where emp.SALARY > 1200

                        select emp).Take(5);

 

        gridEmployee.DataSource = employee;

        gridEmployee.DataBind();

    }

Image3.jpg

Both of the code samples shown above use the Take operator to get the top rows from a result set that is created by a query.

4. Skip Operator

The Skip operator bypasses a specified number of contiguous rows from a sequence/table and returns the remaining table. It can skip rows from the top or can be for a certain criteria, in other words it can also skip rows depending on a certain criteria. It works like NOT IN in SQL.

The following code skips top 5 rows from EMPLOYEE object and getting remaining rows. It shows all rows from EMPLOYEE table except top 5 rows and showing in GridView.

public void GetEmployee()

    {

        EmployeeOperationDataContext employeeContext = new EmployeeOperationDataContext();

 

        var employee = (from emp in employeeContext.EMPLOYEEs                       

                        select emp).Skip(5);

 

        gridEmployee.DataSource = employee;

        gridEmployee.DataBind();

    }

Image4.jpg

The following code skips 2 rows in the Employee table (these employees have a salary less than 1300) and returns the remaining rows. It shows the employee data in a GridView.

private void GetEmployee()

    {

        EmployeeOperationDataContext employeeContext = new EmployeeOperationDataContext();

 

        var employee = (from emp in employeeContext.EMPLOYEEs

                        where emp.SALARY < 1300

                        select emp).Skip(2);

 

        gridEmployee.DataSource = employee;

        gridEmployee.DataBind();

    }

Image5.jpg

Both of the code samples shown above use the Skip operator to skip the specified number of contiguous rows from a result set that is created by a query and returns the remaining rows.

COMMENT USING

Trending up