Thursday, August 28, 2014

Let,Skip,Take Clause in LINQ

 

For example, suppose we have class person the following properties

public class Person
{
    public int EmpId { getset; }
    public string FirstName { getset; }
    public string LastName { getset; }
    public decimal salary { getset; }

}

Let clause

The Let clause enables you to compute values for each query result and reference them by using an alias. The alias can be used in other clauses, such as the Where clause. The Let clause enables you to create a query statement that is easier to read because you can specify an alias for an expression clause included in the query and substitute the alias each time the expression clause is used.


  var queryHike = from p in people
                              let hike = p.salary * 0.1
                              where hike <= 5000
                              select p;
in this query we gave less the  5000. So these type example we can use the let statement

Skip clause
The Skip clause causes a query to bypass elements at the beginning of a results list and return the remaining elements. The number of elements to skip is identified by the count parameter.
var  employee = Employees   .OrderByDescending(e => e.Salary)   .Skip(2)  .First();
in this example  we have find the second  highest salary

Take Clause
The Take clause causes a query to include a specified number of contiguous elements from the start of a results list. The number of elements to include is specified by thecount parameter.
var  employee = Employees   .OrderByDescending(e => e.Salary)   .Skip(10) .take(10);
in this example we can skip first 10 records  and then take next 10 records. In enity framework  we
can use these type of concept for paging

No comments:

Post a Comment

http://blogsiteslist.com