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# IOrderedEnumerable (Query Expression With orderby)

Use a query expression with orderby and then a foreach-loop over the IOrderedEnumerable that is returned.
IOrderedEnumerable. With a query expression (beginning with from) we can sort a collection of elements. When we specify an orderby clause, an IOrderedEnumerable is returned.
Similar to IEnumerable. An IOrderedEnumerable does the same thing an IEnumerable does, and we can use an IOrderedEnumerable variable where an IEnumerable is needed.IEnumerable
An example program. Consider now this program. We have a string array of 3 values. We use a query expression (starting with "from") to sort the values.

And: This returns an IOrderedEnumerable. We can pass IOrderedEnumerable to methods that require IEnumerable.

Note: Usually we do not use IOrderedEnumerable directly—we can just use the variable as though it is an IEnumerable.

C# program that uses IOrderedEnumerable, foreach-loop using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { var animals = new string[] { "bird", "cat", "ant" }; var result = from animal in animals orderby animal ascending select animal; // Our result is an IOrderedEnumerable. // ... We can use it as an IOrderedEnumerable. Test(result); // We can use the IOrderedEnumerable as an IEnumerable. Test2(result); // We can use extension methods for IEnumerable on IOrderedEnumerable. Console.WriteLine("Count: " + result.Count()); } static void Test(IOrderedEnumerable<string> result) { // We can loop over an IOrderedEnumerable. foreach (string value in result) { Console.WriteLine("IOrderedEnumerable: " + value); } } static void Test2(IEnumerable<string> result) { foreach (string value in result) { Console.WriteLine("IEnumerable: " + value); } } } Output IOrderedEnumerable: ant IOrderedEnumerable: bird IOrderedEnumerable: cat IEnumerable: ant IEnumerable: bird IEnumerable: cat Count: 3
Notes. The program shows that "foreach" can be used on IOrderedEnumerable and IEnumerable. This is a common use for these interfaces—looping over them.Foreach

Extensions: The Count() extension method (which loops over the entire collection in some cases) can also be used on IOrderedEnumerable.

Count
For most programs, we can focus on IEnumerable instead of IOrderedEnumerable. The distinction is not important. But it does indicate that the values have been sorted (by an orderby clause).
© 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