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

Left & Right Outer JoinLambda Expression

Left & Right Outer Join Example in LINQ using Lambda Expression

Combines two collections into a single hierarchical collection. The join operation is based on matching keys.This is called group join so lambda expression we can use the group join to achieve the result
Suppose we have two classes’ person and Address
public class Person
{
    public int EmpId { getset; }
    public string FirstName { getset; }
    public string LastName { getset; }
}

public class Address
{
    public int AddressId { getset; }
    public int EmpId { getset; }
    public string CityName { getset; }
    public string StateName { getset; }

}
//insert the data person and address classsed
         List<Person> people = new List<Person> {
            new Person{ EmpId = 1, FirstName = "Abc", LastName = "xyz0" },
            new Person{ EmpId = 2, FirstName = "Abc01", LastName = "xyz1" },
            new Person{ EmpId = 3, FirstName = "Abc02", LastName = "xyz2" },
            new Person{ EmpId = 4,FirstName="Abc03", LastName="xyz3" },
            new Person{ EmpId = 5,FirstName="Abc04", LastName="xyz4" },
            new Person{ EmpId = 6,FirstName="Abc05", LastName="xyz5" } };
        //  List pets = new List { barley, boots, whiskers, bluemoon, daisy };

        List<Address> address = new List<Address> {
            new Address{ AddressId = 1,EmpId=1, CityName = "Delhi", StateName = "Delhi" },
            new Address{ AddressId = 2,EmpId=1 , CityName= "Chandigarh", StateName ="Punjab" },
            new Address{ AddressId = 3, EmpId=1, CityName= "Shimla", StateName = "himachal"},
            new Address{ AddressId = 4,EmpId=2 ,CityName ="kanpur", StateName="Up" },
            new Address{ AddressId = 5,EmpId=4,CityName="other1", StateName="xyz4" },

            new Address{ AddressId = 6, EmpId=2, CityName="others", StateName="xyz5" } };

 



var Query=people.GroupJoin
(address,p=>p.EmpId,a=>a.EmpId,(p,a)=>new{people=p,address=a}).Select(o=>o);

http://blogsiteslist.com