Wednesday, September 27, 2017

Linq- Ordering Operators

The following 5 standard LINQ query operators belong to Ordering Operators category
OrderBy,OrderByDescending , ThenBy , ThenByDescending , Reverse

OrderBy, OrderByDescending, ThenBy, and ThenByDescending can be used to sort data.
Reverse method simply reverses the items in a given collection.

OrderBy or OrderByDescending work fine when we want to sort a collection just by one value or expression. 

If want to sort by more than one value or expression, that's when we use ThenBy or ThenByDescending along with OrderBy or OrderByDescending.

OrderBy or OrderByDescending performs the primary sort. ThenBy or ThenByDescending is used for adding secondary sort. Secondary Sort operators (ThenBy or ThenByDescending ) can be used more than once in the same LINQ query.


Example 1: Sort Students by Name in ascending order
IEnumerable<Student> result = Student.GetAllStudents().OrderBy(s => s.Name);
foreach (Student student in result)
{
    Console.WriteLine(student.Name);
}

Example 2: Rewrite Example 1 using SQL like syntax
IEnumerable<Student> result = from student in Student.GetAllStudents()
                                                      orderby student.Name
                                                      select student;

foreach (Student student in result)
{
    Console.WriteLine(student.Name);
}

Example 3: Sort Students by Name in descending order
IEnumerable<Student> result = Student.GetAllStudents().OrderByDescending(s => s.Name);
foreach (Student student in result)
{
    Console.WriteLine(student.Name);
}

Example 4: Rewrite Example 3 using SQL like syntax
IEnumerable<Student> result = from student in Student.GetAllStudents()
                                                      orderby student.Name descending
                                                      select student;

foreach (Student student in result)
{
    Console.WriteLine(student.Name);
}


Example 1: 
a) Sorts Students first by TotalMarks in ascending order(Primary Sort) 
b) The 4 Students with TotalMarks of 800, will then be sorted by Name in ascending order (First Secondary Sort)
c) The 2 Students with Name of John, will then be sorted by StudentID in ascending order (Second Secondary Sort)

IEnumerable<Student> result = Student.GetAllStudetns()
                                        .OrderBy(s => s.TotalMarks).ThenBy(s => s.Name).ThenBy(s => s.StudentID);

foreach (Student student in result)
{
    Console.WriteLine(student.TotalMarks + "\t" + student.Name + "\t" + student.StudentID);
}

Example 2: Rewrite Example 1 using SQL like syntax. With SQL like syntax we do not use ThenBy or ThenByDescending, instead we specify the sort expressions using a comma separated list. The first sort expression will be used for primary sort and the subsequent sort expressions for secondary sort.
IEnumerable<Student> result = from student in Student.GetAllStudetns()
                                                    orderby student.TotalMarks, student.Name, student.StudentID
                                                      select student;
foreach (Student student in result)
{
    Console.WriteLine(student.TotalMarks + "\t" + student.Name + "\t" + student.StudentID);
}


Example 3: Reverses the items in the collection. 
IEnumerable<Student> students = Student.GetAllStudetns();

Console.WriteLine("Before calling Reverse");
foreach (Student s in students)
{
    Console.WriteLine(s.StudentID + "\t" + s.Name + "\t" + s.TotalMarks);
}

Console.WriteLine();
IEnumerable<Student> result = students.Reverse();

Console.WriteLine("After calling Reverse");
foreach (Student s in result)
{
    Console.WriteLine(s.StudentID + "\t" + s.Name + "\t" + s.TotalMarks);
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More