In a query expression, we use descending and ascending to indicate sort order. Ascending goes from low to high. These C# keywords encourage elegant and clear code.
An example. This program specifies a query expression (starting with from). The parts of query expressions that contain these keywords are query clauses.
Part 1 We create an integer array of odd numbers. We will be sorting these elements with a query expression.
using System;
using System.Linq;
// Part 1: create an integer array.
int[] array = { 1, 3, 5, 7, 9 };
// Part 2: select the elements in a descending order with query clauses.
var result = from element in array
orderby element descending
select element;
// Part 3: evaluate the query and display the results.
foreach (var element in result)
{
Console.WriteLine($"DESCENDING: {element}");
}DESCENDING: 9
DESCENDING: 7
DESCENDING: 5
DESCENDING: 3
DESCENDING: 1
Ascending. There is an ascending keyword that can be used in the same context as the descending keyword (following an orderby clause). Ascending sorts are normally the default.
So You do not need to specify ascending to get an ascending sort. Query expression sorts are implicitly ascending.
However The ascending keyword can provide a symmetry to the orderby clauses in your query expressions.
Detail We create an array of Employee objects. We use a query expression to sort these objects from high to low Salary.
Also If two objects have the same Salary, they are again sorted from low to high Id.
using System;
using System.Linq;
class Employee
{
public int Salary { get; set; }
public int Id { get; set; }
}
class Program
{
static void Main()
{
Employee[] array = new Employee[]
{
new Employee(){Salary = 40000, Id = 4},
new Employee(){Salary = 40000, Id = 0},
new Employee(){Salary = 60000, Id = 7},
new Employee(){Salary = 60000, Id = 9}
};
// Highest salaries first.// ... Lowest IDs first.
var result = from em in array
orderby em.Salary descending, em.Id ascending
select em;
foreach (var em in result)
Console.WriteLine("{0}, {1}", em.Salary, em.Id);
}
}60000, 7
60000, 9
40000, 0
40000, 4
Multiple properties. Query expressions provide an intuitive syntax for sorting on 2 properties at once. The first property specified in the orderby clause is the primary sort.
And The second is the secondary sort that is only activated when a conflict occurs.
Ascending, notes. Because ascending is the default, you don't need to specify it. You can just omit this keyword and the query expression will function the same way.
Contextual. The descending keyword is only considered a keyword in certain cases. The C# compiler provides a special-cased parser for query expressions.
Note Query syntax was provided to allow for more natural syntax on declaration expressions.
A summary. Descending and ascending indicate the progression of an orderby clause in a query expression. The query clause is translated to method syntax before being executed.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 29, 2023 (edit link).