C# LINQ orderby Clause
In a LINQ query expression, the orderby clause enables you to sort the result sequence into the ascending or descending order based on the default comparer for the element type. The orderby clause may have multiple keys in order to perform the sorting based on different sort operations. By default it sorts the result in ascending order that generates the sequence from smallest to the largest element. The method based syntax of LINQ also allows to specify the custom comparer sorting.
Syntax for LINQ orderby Clause
from <range-variable> in <data source sequence object> orderby <range-variable> [ascending]|[descending] select <range-variable>
The above syntax shows how to add the orderby clause expression inside the query to sort the results. It also supports two contextual keywords such as ascending and descending. You can use either ascending or descending to sort the result sequence in the specified manner. By default it uses the ascending keyword value that can be omitted.
Example for LINQ orderby Clause using C# in ASP.Net
int[] numbersArray = { 37, 40, 20, 23, 25, 27, 30, 5, 7, 10, 13, 33, 35, 15, 17 };
var numbers = from n in numbersArray
where n % 5 == 0
orderby n
select n;
foreach (int number in numbers)
{
Response.Output.Write("{0} ", number);
}
// Output:
// 5 10 15 20 25 30 35 40
In the above C# LINQ orderby clause example, first we have used the where clause to filter the results to get the numbers divisible by 5 and further orderby clause has been used to sort the sequence into ascending order by default.
Continue to next tutorial: LINQ join Clause using C# in ASP.Net to learn how to associate the elements of multiple data source sequences.

* will not be published
* hint: http://www.example.com