Order By 子句 (Visual Basic)Order By Clause (Visual Basic)

指定查询结果的排序顺序。Specifies the sort order for a query result.

语法Syntax

Order By orderExp1 [ Ascending | Descending ] [, orderExp2 [...] ]  

部件Parts

orderExp1
必需。Required. 当前查询结果中的一个或多个字段,用于确定如何对返回的值进行排序。One or more fields from the current query result that identify how to order the returned values. 字段名称必须用逗号(,)分隔。The field names must be separated by commas (,). 您可以通过使用 AscendingDescending 关键字,按升序或降序来识别每个字段。You can identify each field as sorted in ascending or descending order by using the Ascending or Descending keywords. 如果未指定 AscendingDescending 关键字,则默认排序顺序为升序。If no Ascending or Descending keyword is specified, the default sort order is ascending. 排序顺序字段的优先级从左到右。The sort order fields are given precedence from left to right.

备注Remarks

您可以使用 Order By 子句对查询结果进行排序。You can use the Order By clause to sort the results of a query. Order By 子句只能根据当前作用域的范围变量对结果进行排序。The Order By clause can only sort a result based on the range variable for the current scope. 例如,Select 子句在查询表达式中引入了新的作用域,该作用域具有新的迭代变量。For example, the Select clause introduces a new scope in a query expression with new iteration variables for that scope. 在查询中的 Select 子句之前定义的范围变量在 Select 子句之后不可用。Range variables defined before a Select clause in a query are not available after the Select clause. 因此,如果要按 Select 子句中不存在的字段对结果进行排序,则必须将 Order By 子句置于 Select 子句之前。Therefore, if you want to order your results by a field that is not available in the Select clause, you must put the Order By clause before the Select clause. 需要执行此操作的一个示例是,如果要按不作为结果的一部分返回的字段对查询进行排序。One example of when you would have to do this is when you want to sort your query by fields that are not returned as part of the result.

字段的升序和降序由字段的数据类型的 IComparable 接口的实现确定。Ascending and descending order for a field is determined by the implementation of the IComparable interface for the data type of the field. 如果数据类型未实现 IComparable 接口,则忽略排序顺序。If the data type does not implement the IComparable interface, the sort order is ignored.

示例Example

下面的查询表达式使用 From 子句为 books 集合声明范围变量 bookThe following query expression uses a From clause to declare a range variable book for the books collection. Order By 子句按价格升序对查询结果进行排序(默认值)。The Order By clause sorts the query result by price in ascending order (the default). 价格相同的书籍按标题的升序排序。Books with the same price are sorted by title in ascending order. Select 子句选择 Title 并将 Price 属性作为查询返回的值。The Select clause selects the Title and Price properties as the values returned by the query.

Dim titlesAscendingPrice = From book In books
                           Order By book.Price, book.Title
                           Select book.Title, book.Price

示例Example

下面的查询表达式使用 Order By 子句按照价格以降序对查询结果进行排序。The following query expression uses the Order By clause to sort the query result by price in descending order. 价格相同的书籍按标题的升序排序。Books with the same price are sorted by title in ascending order.

Dim titlesDescendingPrice = From book In books
                            Order By book.Price Descending, book.Title
                            Select book.Title, book.Price

示例Example

下面的查询表达式使用 Select 子句来选择书名、价格、发布日期和作者。The following query expression uses a Select clause to select the book title, price, publish date, and author. 然后,它将填充新范围的范围变量的 TitlePricePublishDateAuthor 字段。It then populates the Title, Price, PublishDate, and Author fields of the range variable for the new scope. Order By 子句按作者姓名、书籍标题和价格对新范围变量进行排序。The Order By clause orders the new range variable by author name, book title, and then price. 每列按默认顺序(升序)排序。Each column is sorted in the default order (ascending).

Dim bookOrders =
  From book In books
  Select book.Title, book.Price, book.PublishDate, book.Author
  Order By Author, Title, Price

另请参阅See also