Query Expression Syntax Examples: Ordering

 

The examples in this topic demonstrate how to use the OrderBy and OrderByDescending methods to query the AdventureWorks Sales Model using query expression syntax. The AdventureWorks Sales Model used in these examples is built from the Contact, Address, Product, SalesOrderHeader, and SalesOrderDetail tables in the AdventureWorks sample database.

The examples in this topic use the following using/Imports statements:

[!CODE [DP L2E Examples#ImportsUsing](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#importsusing)]

Example

The following example uses OrderBy to return a list of contacts ordered by last name.

[!CODE [DP L2E Examples#OrderBySimple1](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#orderbysimple1)]

Example

The following example uses OrderBy to sort a list of contacts by length of last name.

[!CODE [DP L2E Examples#OrderBySimple2](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#orderbysimple2)]

Example

The following example uses orderby… descending (Order By … Descending in Visual Basic), which is equivalent to the OrderByDescending method, to sort the price list from highest to lowest.

[!CODE [DP L2E Examples#OrderByDescendingSimple1](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#orderbydescendingsimple1)]

Example

The following example uses OrderBy and ThenBy to return a list of contacts ordered by last name and then by first name.

[!CODE [DP L2E Examples#OrderByThenBy](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#orderbythenby)]

Example

The following example uses OrderBy… Descending, which is equivalent to the ThenByDescending method, to sort a list of products, first by name and then by list price from highest to lowest.

[!CODE [DP L2E Examples#ThenByDescendingSimple](../CodeSnippet/VS_Snippets_Data/DP L2E Examples#thenbydescendingsimple)]

Queries in LINQ to Entities

Show: