Tuesday, 14 May 2013

Linq tutorial for Beginners

What is LINQ?
LINQ stands for Language integrated Query, that allows us to query local object collection or remote data source.

Main Points:
1. Linq data has two parts, sequence and elements. Here names is a sequence and array members are elements.
2. Linq doesn't alter the input sequence. So result will always be in the order it was input.
3. Linq always return an IEnumerable result.
4. "n => n.EndsWith("a")" is a lambda expression that filters the result for us. 

Examples:
// Basic Query
// To achieve  Select * From Product  you need to write follofing linq Query
var Result1 = from p in db.Product
                 select p;

// To achieve  Select ProductID, ProductName, Price From Product you need to write follofing linq Query
    var Result = from p in db.Product
                 select new {
                     p.ProductID,
                     p.ProductName,
                     p.Price
                 };



// Where Clause Query
// To achieve  Select * From Product Where ProductID = 1 you need to write follofing linq Query  
  var Result = from p in db.Product
                 where p.ProductID == 1
                 select p;



// To achieve Select * From Product Where SupplierId =3 and Price > 10 you need to write follofing linq Query    
var Result = from p in db.Product
                 where p.SupplierID == 3 && p.Price > 10
                 select p;




// To achieve Select * From Product Where SupplierId =2 Or SupplierId=5 you need to write follofing linq Query

    var Result = from p in db.Product
                 where p.SupplierID == 2 || p.SupplierID == 5
                 select p;



// Order By Query
//To achieve   Select * From Product Order By ProductId you need to write follofing linq Query

    var Result = from p in db.Product
                 orderby p.ProductID
                 select p;



// To achieve  Select * From Product Order By ProductId Desc you need to write follofing linq Query

    var Result = from p in db.Product
                 orderby p.ProductID descending
                 select p;




// To achieve  Select * From Product Order By CategoryId, Price Desc you need to write follofing linq Query   
var Result = from p in db.Product
                 orderby p.CategoryID, p.Price descending
                 select p;



// Top Query
//To achieve Select Top 5 * From Product you need to write follofing linq Query

    var Result = (from p in db.Product
                 select p).Take(5);



//To achieve Select Top 1 * From Product you need to write follofing linq Query

    var Result = (from p in db.Product
                   select p).Take(1);
     or

    var Result = (from p in db.Product
                   select p).First();



// Distinct Query
//To achieve Select Distinct CategoryId From Product you need to write follofing linq Query

    var Result = (from p in db.Product
                   select p.CategoryID).Distinct();



// Group By Query
//To achieve Select CategoryId, Count(CategoryID) As FieldName From Product Group By CategoryId you need to write follofing linq Query

    var Result = from p in db.Product
                  group p by p.CategoryID into g
                  select new {
                      CategoryId = g.Key,
                      FieldName = g.Count()
                  };



//To achieve Select CategoryId, Avg(UnitPrice) As NewField From Product Group By CategoryId you need to write follofing linq Query

    var Result = from p in db.Product
                  group p by p.CategoryID into g
                  select new {
                      CategoryId = g.Key,
                      FieldName = g.Average(S => S.Price)
                  };



// Union Query
//To achieve Select * From Product Where CategoryId =1 union Select *  From Product Where CategoryId = 2 you need to write follofing linq Query

    var Result = (from p in db.Product
                   where p.CategoryID == 1
                   select p).Union(
                       from m in db.Product
                       where m.CategoryID == 2
                       select m
                   );

1 comment:

  1. Thank you Sagar,
    It is great help to me.
    really you are doing great job.

    ReplyDelete