ObjectQuery<T>.OrderBy(String, ObjectParameter[]) 方法
定義
依據指定的準則,排序查詢結果。Orders the query results by the specified criteria.
public:
System::Data::Objects::ObjectQuery<T> ^ OrderBy(System::String ^ keys, ... cli::array <System::Data::Objects::ObjectParameter ^> ^ parameters);
public System.Data.Objects.ObjectQuery<T> OrderBy (string keys, params System.Data.Objects.ObjectParameter[] parameters);
member this.OrderBy : string * System.Data.Objects.ObjectParameter[] -> System.Data.Objects.ObjectQuery<'T>
Public Function OrderBy (keys As String, ParamArray parameters As ObjectParameter()) As ObjectQuery(Of T)
參數
- keys
- String
排序結果所依據的索引鍵資料行。The key columns by which to order the results.
- parameters
- ObjectParameter[]
這個方法所使用的零個或多個參數。Zero or more parameters that are used in this method.
傳回
新的 ObjectQuery<T> 執行個體,它就相當於套用了 ORDER BY 的原始執行個體。A new ObjectQuery<T> instance that is equivalent to the original instance with ORDER BY applied.
例外狀況
keys
或 parameters
參數為 null
。The keys
or parameters
parameter is null
.
key
為空字串。The key
is an empty string.
範例
這個範例是根據 Microsoft SQL Server 產品範例: Database。This example is based on the Microsoft SQL Server Product Samples: Database. 此範例會建立新 ObjectQuery<T> 的物件,其中包含常設查詢順序的結果 ProductID
。The example creates a new ObjectQuery<T> object that contains the results of the existing query order by ProductID
.
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
string queryString = @"SELECT VALUE product
FROM AdventureWorksEntities.Products AS product";
ObjectQuery<Product> productQuery1 =
new ObjectQuery<Product>(queryString,
context, MergeOption.NoTracking);
ObjectQuery<Product> productQuery2 =
productQuery1.OrderBy("it.ProductID");
// Iterate through the collection of Product items.
foreach (Product result in productQuery2)
{
Console.WriteLine("{0}", result.ProductID);
}
}
Using context As New AdventureWorksEntities()
Dim queryString As String = "SELECT VALUE product FROM AdventureWorksEntities.Products AS product"
Dim productQuery1 As New ObjectQuery(Of Product)(queryString, context, MergeOption.NoTracking)
Dim productQuery2 As ObjectQuery(Of Product) = productQuery1.OrderBy("it.ProductID")
' Iterate through the collection of Product items.
For Each result As Product In productQuery2
Console.WriteLine("{0}", result.ProductID)
Next
End Using
備註
無法確保結果在巢狀查詢中的排序。The ordering of results in a nested query cannot be guaranteed.
OrderBy 應該一律是序列中的最後一個查詢產生器方法。OrderBy should always be the final query builder method in the sequence.