In this article, I will demonstrate how we can use OrderBy, OrderByDescending, ThenBy, ThenByDescending and Reverse operator of Language-Integrated Query (LINQ). OrderBy, OrderByDescending, ThenBy, ThenByDescending and Reverse operators are part of sorting data in a Language-Integrated Query (LINQ). A sorting operation orders the elements of a sequence based on one or more attributes. The first sort criterion performs a primary sort on the elements. By specifying a second sort criterion, you can sort the elements within each primary sort group.
- CREATE TABLE [dbo].[Employee](
- [Employee_Id] [int] IDENTITY(1,1) NOT NULL,
- [First_Name] [nvarchar](50) NULL,
- [Last_Name] [nvarchar](50) NULL,
- [Salary] [int] NULL,
- [Joing_Date] [nvarchar](50) NULL,
- [Department] [nvarchar](50) NULL,
- CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
- (
- [Employee_Id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
-
- GO
Step 2
Open Visual Studio 2015 and create a new console application with a meaningful name.
Step 3
Add Entity Framework now. For that, right click on Models folder, select Add, then select New Item, then click on it.
Screenshot for adding entity framework 1
After clicking on the new item, you will get a window; from there, select Data from the left panel and choose ADO.NET Entity Data Model, give it the name DBModels (this name is not mandatory you can give any name) and click on Add.
Screenshot for adding entity framework 2
After you click on "Add a window", the wizard will open, choose EF Designer from the database and click Next.
Screenshot for adding entity framework 3
After clicking on Next a window will appear. Choose New Connection. Another window will appear, add your server name if it is local then enter dot (.). Choose your database and click on OK.
Screenshot for adding entity framework 4
Connection will be added. If you wish save connect as you want. You can change the name of your connection below. It will save connection in web config then click on Next.
Screenshot for adding entity framework 5
After clicking on NEXT another window will appear, choose database table name as shown in the below screenshot, then click on Finish.
Screenshot for adding entity framework 6
Entity framework will be added and respective class gets generated under Models folder.
Screenshot for adding entity framework 7
Following class will be added
- namespace SortingData_Demo
- {
- using System;
- using System.Collections.Generic;
-
- public partial class Employee
- {
- public int Employee_Id { get; set; }
- public string First_Name { get; set; }
- public string Last_Name { get; set; }
- public Nullable<int> Salary { get; set; }
- public string Joing_Date { get; set; }
- public string Department { get; set; }
- }
- }
Step 4
Write a program to call database class
Example of OrderBy
- using System;
- using System.Collections.Generic;
- using System.Linq;
-
- namespace SortingData_Demo
- {
- class Program
- {
-
- static void Main(string[] args)
- {
- using (DBModel db = new DBModel())
- {
-
- IEnumerable<Employee> employeeList = db.Employees.ToList();
-
- Console.WriteLine("EMPLOYEE LIST BEFOR SORTING \n");
-
- foreach (Employee emp in employeeList)
- {
- Console.WriteLine(emp.Employee_Id+"\t"+emp.First_Name + "\t" + emp.Last_Name + "\t" +emp.Salary+ "\t" +emp.Joing_Date + "\t" +emp.Department);
-
- }
-
- Console.WriteLine(" \n");
- Console.WriteLine("EMPLOYEE LIST AFTER SORTING \n");
-
- var result = employeeList.OrderBy(e => e.First_Name);
-
- foreach (var emp in result)
- {
- Console.WriteLine(emp.Employee_Id + "\t" + emp.First_Name);
- }
- }
-
- }
- }
- }
Output
Example of OrderByDescending