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. 返される値の順序付け方法を示す、現在のクエリ結果の 1 つまたは複数のフィールド。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 (,). Ascending
または Descending
キーワードを使用して、昇順または降順で並べ替えて各フィールドを識別できます。You can identify each field as sorted in ascending or descending order by using the Ascending
or Descending
keywords. Ascending
または Descending
キーワードが指定されていない場合、既定の並べ替え順序は昇順です。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.
RemarksRemarks
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. これを行う必要がある場合の 1 つの例は、結果の一部として返されないフィールドによってクエリを並べ替える場合です。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
コレクションに対して book
範囲変数を宣言しています。The 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. 次に、新しいスコープの範囲変数の Title
、Price
、PublishDate
、および Author
の各フィールドを設定します。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