Ordering Operators

Ordering operators allow sorting query results according to the given condition. Within LINQ queries this is accomplished via the Order By clause. This clause allows ordering query results in both ascending and descending fashions, where ascending is the default way. The following example sorts the query result so that products are ordered from the one that has the lowest unit price to the one having the highest unit price:

image

To get a result ordered from the highest value to the lowest, you use the Descending keyword as follows:

image

This can shape the query result opposite of the first example. You can also provide more than one Order By clause to get subsequent ordering options. Using extension methods provides a little bit more granularity in ordering results. For example you can use the OrderBy and ThenBy extension methods for providing multiple ordering options as demonstrated in the following code:

Dim query = products.OrderBy(Function(p) p.UnitPrice).
ThenBy(Function(p) p.ProductName)

As usual, both methods take lambdas as arguments. There are also OrderByDescending and ThenByDescending extension methods that order the result from the highest value to the lowest. The last ordering method is Reverse that reverses the query result and that you can simply use as follows:

Dim revertedQuery = query.Reverse()

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

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