Linq Order by Descending Query

LINQ Queries Examples Using Query Method and Query Syntax

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,





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!
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

LINQ Queries Examples Using Query Method and Query Syntax LINQ Queries Examples Using Query Method and Query Syntax Reviewed by Anil Singh on 10:33 PM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^