TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# orderby Query Keyword

Use the orderby keyword in a query expression. Include System.Linq.
Orderby. An orderby clause adds sorting to a query. It imposes a sorting algorithm to the expression's result. It can be placed within a query that also does other things.KeywordsSort
Notes, method. In the C# language, this clause is compiled to the OrderBy method. But often the clearest way to use the OrderBy method is with a clause.
First example. The "using System.Linq" directive is present at the top. The 3 query expressions use an orderby element, orderby element ascending, and orderby element descending syntax.LINQ

Info: The identifier "element" is entirely arbitrary, and it is just declared in each individual query.

Ascending: We see that the first query expression and the second query expression both perform an ascending sort.

Tip: The word ascending means "lowest to highest", like how a staircase ascends from the lowest step to the highest step.

And: The final query returns a descending sort, with the highest numbers going to the lowest numbers. This is the logical opposite.

Descending
C# program that uses orderby clause using System; using System.Linq; class Program { static void Main() { // Input array. int[] array = { 2, 5, 3 }; // Use orderby, orderby ascending, and orderby descending. var result0 = from element in array orderby element select element; var result1 = from element in array orderby element ascending select element; var result2 = from element in array orderby element descending select element; // Print results. Console.WriteLine("result0"); foreach (var element in result0) { Console.WriteLine(element); } Console.WriteLine("result1"); foreach (var element in result1) { Console.WriteLine(element); } Console.WriteLine("result2"); foreach (var element in result2) { Console.WriteLine(element); } } } Output result0 2 3 5 result1 2 3 5 result2 5 3 2
Compiler. How are the orderby clauses in the expressions compiled to the intermediate form? The C# specification describes how the query clauses are translated into extension method calls.

System.Linq: The extension method calls are why the System.Linq namespace is required.

Info: The extensions OrderBy, and OrderByDescending are used when the C# compiler parses query expressions.

OrderBy, OrderByDescending
A summary. We used query expressions with ascending and descending, resulting in 3 sorted and enumerable collections. We examined the query syntax intermediate form.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf