The orderby operator

The orderby operator is used to sort your data in ascending or descending order. The following code shows how to sort the data in descending order:

int[] dataElements = { 8, 11, 6, 3, 9 };
var resultOrder = from dataElement in dataElements
where dataElement > 5
orderby dataElement descending
select dataElement;
Console.WriteLine(string.Join(", ", resultOrder));

In the preceding code, we have declared an array of integers. Now, from this array, we select all numbers that are greater than 5. After selecting them, we sort them in descending order using the orderby clause. Finally, we print them. The following is the output of the program when it's executed:

Note that, in the preceding output, the numbers are in descending order and all are greater than 5. In the next section, we will look at the Average operator in LINQ.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.40.63