A Lambda expression is an anonymous function that
we can use to create expression tree types, delegates etc.
A lambda expression is an expression on the right
side of the => operator is called an expression lambda. It is also called (=>)
goes to operator.
Syntax: (Input parameters) => Expirations or Statement
block
A Lambda expression is particularly helpful for
writing LINQ query expressions.
A lambda expression is powerful, shorter and
suitable tool.
Personally, I
prefer to lambda extensions for most code writing. I am only using the statements
if I am doing LINQ to SQL otherwise I am trying to emulate SQL. I find that the
lambda methods flow better with code whereas the statements are visually
distracting.
So, here I am trying to explain gather T-SQL
queries along with their LINQ queries with lambda expressions.
EmpEntites
empEnt = new EmpEntites(); //This is entities
objects.
In these entities objects, we have a table “tbl_Employee” and I am trying to explain
T-SQL queries with their LINQ queries with lambda expressions.
Now, I get all the records from “tbl_Employee” which are sorted by EmpId ascending i.e.
var result = empEnt.tbl_Employee.OrderBy(x=>x.EmpId).ToList(); // This is Lambda.
Now, I get all the records from “tbl_Employee” which are sorted by EmpId descending i.e.
var result = empEnt.tbl_Employee.OrderByDescending(x=>x.EmpId).ToList(); // This is Lambda.
Remember Points,
1. A lambda expression is use to execute
any unsafe code inside any lambda expression.
2. A lambda expression can return a
value and may have parameters.
3. A lambda expression is not meant to
be used on the operators left side.