Basically, the LINQ queries using query syntax
and method syntax. The method syntax called Lambda Expressions.
The LINQ query syntax is close to native language
and it is simple and easy to read.
The method syntax is powerful but it is shorter
and suitable tool.
They compile the same and both are equivalent.
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.
You can see the LINQ Query in the below pic,
You can see the LINQ Query in the below pic,
So, here I am trying to explain gather T-SQL queries along with their equivalent LINQ queries in the both method and query syntax.
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 along with their equivalent LINQ queries in the both method and
query syntax.
Now, get top two records from “tbl_Employee” i.e.
var
result = empEnt.tbl_Employee.Take(2).ToList(); //This is Lambda.
var
result = from emp in empEnt.tbl_Employee.Take(2) select emp; //This is query.
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.
var result = from emp in empEnt.tbl_Employee orderby(emp.EmpId) select emp; //This is query.
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.
var result = from emp in empEnt.tbl_Employee orderby(emp.EmpId) descending select emp; //This is query.
I hope this is helpful to you! Thank you!