In LINQ, the sorting operators are used to sort data in ascending or descending order based on one or more attributes in Collection or SQL Server database.
There are following are the different types of sorting operators.
Operator in LINQ
methods in LINQ
details of operator
orderby
orderby()
sort data by default in ascending order
orderby ... descending
OrderByDescending()
sort data by default in descending order
orderby ... , ...
ThenBy()
this sort is used for secondary sorting by default ascending order
orderby ... , ... descending
ThenByDescending()
this sort is used for secondary sorting by default descending order
N/A
Reverse()
this sort the data in reverse order
orderby and orderby ... descending
Syntax
datatype variable-name = from object in collection where condition orderby condition select student ;
datatype variable-name = from object in collection
where condition
orderby condition descending select student ;
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<Student> students = new List<Student>()
{
new Student() { StudentId = 1, Name = 'Asu', Marks = 500 },
new Student() { StudentId = 2, Name = 'Shyam', Marks = 300 },
new Student() { StudentId = 3, Name = 'Shriyam', Marks = 400 },
new Student() { StudentId = 4, Name = 'Sunny', Marks = 550 },
new Student() { StudentId = 5, Name = 'Ram', Marks = 600 },
new Student() { StudentId = 6, Name = 'Krishna', Marks = 700 },
new Student() { StudentId = 7, Name = 'Anupam', Marks = 550 }
};
// select projection using first sysntax
var result1= from student in students
where student.Marks > 300 && student.Marks < 700
orderby student.Marks
select student ;
Console.WriteLine('Accending order\n');
Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
result1.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
var result2= from student in students
where student.Marks > 300 && student.Marks < 700
orderby student.Marks descending
select student ;
Console.WriteLine('\nDescending order\n');
Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
result2.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
var result3 = students.Where( student => student.StudentId < 5).OrderBy(stu => stu.Marks);
Console.WriteLine('\nAccending order using where method Syntax :\n');
Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
result3.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
Console.ReadLine();
}
}
class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int Marks { get; set; }
}
Output:
Accending order
Name ID Marks
Shriyam 3 400
Asu 1 500
Sunny 4 550
Anupam 7 550
Ram 5 600
Descending order
Name ID Marks
Ram 5 600
Sunny 4 550
Anupam 7 550
Asu 1 500
Shriyam 3 400
Accending order using where method Syntax :
Name ID Marks
Shyam 2 300
Shriyam 3 400
Asu 1 500
Sunny 4 550
thenby and theneby... descending
Syntax
datatype variable-name = from object in collection
where condition orderby condition, condition select student ;
datatype variable-name = from object in collection
where condition
orderby condition, condition descending
select student ;
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<Student> students = new List<Student>()
{
new Student() { StudentId = 1, Name = 'Asu', Marks = 500 },
new Student() { StudentId = 2, Name = 'Shyam', Marks = 300 },
new Student() { StudentId = 3, Name = 'Shriyam', Marks = 400 },
new Student() { StudentId = 4, Name = 'Sunny', Marks = 550 },
new Student() { StudentId = 5, Name = 'Ram', Marks = 600 },
new Student() { StudentId = 6, Name = 'Krishna', Marks = 700 },
new Student() { StudentId = 7, Name = 'Anupam', Marks = 550 }
};
// select projection using first sysntax
var result1= from student in students
where student.Marks > 300 && student.Marks < 700
orderby student.Marks, student.StudentId
select student ;
Console.WriteLine('Accending order\n');
Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
result1.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
var result2= from student in students
where student.Marks > 300 && student.Marks < 700
orderby student.Marks,student.StudentId descending
select student ;
Console.WriteLine('\nDescending order\n');
Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
result2.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
var result3 = students.Where( student => student.StudentId < 5).OrderBy(stu => stu.Marks).ThenBy( stu => stu.StudentId);
Console.WriteLine('\nAccending order using where method Syntax :\n');
Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
result3.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
Console.ReadLine();
}
}
class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int Marks { get; set; }
}
Output
Accending order
Name ID Marks
Shriyam 3 400
Asu 1 500
Sunny 4 550
Anupam 7 550
Ram 5 600
Descending order
Name ID Marks
Shriyam 3 400
Asu 1 500
Anupam 7 550
Sunny 4 550
Ram 5 600
Accending order using where method Syntax :
Name ID Marks
Shyam 2 300
Shriyam 3 400
Asu 1 500
Sunny 4 550
Reverse
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<Student> students = new List<Student>()
{
new Student() { StudentId = 1, Name = 'Asu', Marks = 500 },
new Student() { StudentId = 2, Name = 'Shyam', Marks = 300 },
new Student() { StudentId = 3, Name = 'Shriyam', Marks = 400 },
new Student() { StudentId = 4, Name = 'Sunny', Marks = 550 },
new Student() { StudentId = 5, Name = 'Ram', Marks = 600 },
new Student() { StudentId = 6, Name = 'Krishna', Marks = 700 },
new Student() { StudentId = 7, Name = 'Anupam', Marks = 550 }
};
var result3 = students.Where( student => student.StudentId < 5).Reverse();
Console.WriteLine('\nReverse order using where method Syntax :\n');
Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
result3.ToList().ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
Console.ReadLine();
}
}
class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int Marks { get; set; } }
Output
Reverse order using where method Syntax :
Name ID Marks
Sunny 4 550
Shriyam 3 400
Shyam 2 300
Asu 1 500
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In LINQ, the sorting operators are used to sort data in ascending or descending order based on one or more attributes in Collection or SQL Server database.
There are following are the different types of sorting operators.
orderby and orderby ... descending
Syntax
Example
Output:
thenby and theneby... descending
Syntax
Example
Output
Reverse
Output