Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

LINQ - Filtering and Sorting in C#

  1. using System;
  2. using System.Linq;
  3.  
  4. class Program
  5. {
  6. static void Main()
  7. {
  8. int[] array = { 1, 2, 3, 6, 7, 8 };
  9. // Query expression.
  10. var elements = from element in array
  11. orderby element descending
  12. where element > 2
  13. select element;
  14. // Enumerate.
  15. foreach (var element in elements)
  16. {
  17. Console.Write(element);
  18. Console.Write(' ');
  19. }
  20. Console.WriteLine();
  21. }
  22. }


In the above example, we are filtering the elements which greater than 2 and sorting the elements by descending order.

Output:
8763