Thursday, November 29, 2012

LINQ and equivalent LAMDA expression


This discussion I will try to provide some common LINQ statement and the equivalent LAMDA expression. This is purely personal preference to choose LINQ or Lamda. I am not going to describe which one is the best way considering performance as parameter. What I noticed both emit the same IL code after compilation. I used Entity framework 5.0 and Northwind database for this sample.

Retrieve all records
LINQ
from c in dbContext.Categories
       select c;
LAMDA
dbContext.Categories.Select(c => c);

Specific fields
LINQ
from c in dbContext.Categories
       select new { c.CategoryName, c.CategoryID };
LAMDA
dbContext.Categories.Select(c => new { c.CategoryName, c.CategoryID })

Orderby asc
LINQ
from c in dbContext.Categories
       orderby c.CategoryName
       select c;
LAMDA
dbContext.Categories.OrderBy(c => c.CategoryName);

Orderby dsc
LINQ
from c in dbContext.Categories
       orderby c.CategoryName descending
       select c;
LAMDA
      dbContext.Categories.OrderByDescending(c => c.CategoryName);

Multiple orderby
LINQ
from c in dbContext.Categories
       orderby c.CategoryName , c.CategoryID
       select c;
LAMDA
      dbContext.Categories.OrderBy(c => c.CategoryName).ThenBy(c => c.CategoryID);

Orderby asc and dec
LINQ
from c in dbContext.Categories
       orderby c.CategoryName, c.CategoryID descending
       select c;
LAMDA
dbContext.Categories.OrderBy(c => c.CategoryName)
.ThenByDescending(c => c.CategoryID);

Filter
LINQ
from c in dbContext.Categories
       where c.CategoryID.Equals(1)
       select c;
LAMDA
dbContext.Categories.Where(c => c.CategoryID.Equals(1));

Filter with multiple criteria
LINQ
from c in dbContext.Categories
       where c.CategoryID.Equals(1) && c.CategoryName.StartsWith("A")
       select c;
LAMDA
dbContext.Categories.Where(c => c.CategoryID.Equals(1) &&
       c.CategoryName.StartsWith(
"A"));

Group By
LINQ
from c in dbContext.Categories
       group c by c.CategoryName into g
       select g;
LAMDA
dbContext.Categories.GroupBy(c => c.CategoryName)

Multiple group By
LINQ
from c in dbContext.Categories
       group c by new { c.CategoryID, c.CategoryName } into g
       select g;
LAMDA
dbContext.Categories.GroupBy(c => new { c.CategoryID, c.CategoryName })


Record Count
LINQ
(from c in dbContext.Categories
        where c.CategoryName.StartsWith("B")
       select c).Count();
LAMDA
dbContext.Categories.Count(c => c.CategoryName.StartsWith("B"));

Join
LINQ
from c in dbContext.Categories
       join p in dbContext.Products1 on c.CategoryID equals p.CategoryID
       select new
              {
                 CategoryName,
                 p.ProductID,
                 p.ProductName
              };
LAMDA
dbContext.Categories.Join(dbContext.Products1, c => c.CategoryID, P =>
       P.CategoryID,((c,p)=>
new {c.CategoryName,p.ProductID,p.ProductName}));

No comments:

Post a Comment

File recovery from VM backup(Recovery Service vault)

Backup service can be enabled on VM through recovery service vault. This will primarily facilitate restoration of a VM from Backup when disa...