ObjectQuery<T>.Top(String, ObjectParameter[]) メソッド

定義

クエリの結果を、指定されたアイテム数に制限します。Limits the query results to a specified number of items.

public:
 System::Data::Objects::ObjectQuery<T> ^ Top(System::String ^ count, ... cli::array <System::Data::Objects::ObjectParameter ^> ^ parameters);
public System.Data.Objects.ObjectQuery<T> Top (string count, params System.Data.Objects.ObjectParameter[] parameters);
member this.Top : string * System.Data.Objects.ObjectParameter[] -> System.Data.Objects.ObjectQuery<'T>
Public Function Top (count As String, ParamArray parameters As ObjectParameter()) As ObjectQuery(Of T)

パラメーター

count
String

結果に文字列として返されるアイテムの数。The number of items in the results as a string.

parameters
ObjectParameter[]

解析時にスコープに含める必要があるオプションのクエリ パラメーターのセット。An optional set of query parameters that should be in scope when parsing.

戻り値

TOP が適用された元のインスタンスに相当する、新しい ObjectQuery<T> インスタンス。A new ObjectQuery<T> instance that is equivalent to the original instance with TOP applied.

例外

countnullです。count is null.

count が空の文字列です。count is an empty string.

これらの例は、 AdventureWorks Sales Modelに基づいています。These examples are based on the AdventureWorks Sales Model.

この例では、既存のクエリの最初の 2 つの結果を含む ObjectQuery<T> を新たに作成します。This example creates a new ObjectQuery<T> that contains the first two results of the existing query.

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.Top("2");

    // Iterate through the collection of Product items.
    foreach (Product result in productQuery2)
        Console.WriteLine("{0}", result.Name);
}
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.Top("2")

    ' Iterate through the collection of Product items. 
    For Each result As Product In productQuery2
        Console.WriteLine("{0}", result.Name)
    Next
End Using

この例ではProduct 、クエリ結果の最初の3つをでProduct.ListPrice並べ替えた後、5つのオブジェクトを取得します。This example gets five Product objects after skipping the first three in the query result, sorted by Product.ListPrice. Topは、ページングの制限の代わりに使用されます。Top is used instead of LIMIT for paging.

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Define the parameters used to define the "page" of returned data.
    int skipValue = 3;
    int limitValue = 5;

    // Define a query that returns a "page" or the full 
    // Product data using the Skip and Top methods. 
    // When Top() follows Skip(), it acts like the LIMIT statement.
    ObjectQuery<Product> query = context.Products
        .Skip("it.ListPrice", "@skip",
                new ObjectParameter("skip", skipValue))
        .Top("@limit", new ObjectParameter("limit", limitValue));

    // Iterate through the page of Product items.
    foreach (Product result in query)
        Console.WriteLine("ID: {0}; Name: {1}",
        result.ProductID, result.Name);
}
Using context As New AdventureWorksEntities()
    ' Define the parameters used to define the "page" of returned data. 
    Dim skipValue As Integer = 3
    Dim limitValue As Integer = 5

    ' Define a query that returns a "page" or the full 
    ' Product data using the Skip and Top methods. 
    ' When Top() follows Skip(), it acts like the LIMIT statement. 
    Dim query As ObjectQuery(Of Product) = _
        context.Products.Skip("it.ListPrice", "@skip", _
            New ObjectParameter("skip", skipValue)).Top("@limit", New ObjectParameter("limit", limitValue))

    ' Iterate through the page of Product items. 
    For Each result As Product In query
        Console.WriteLine("ID: {0}; Name: {1}", result.ProductID, result.Name)
    Next
End Using

注釈

Top は、クエリが並べ替えられていない限り、非決定的です。Top is nondeterministic unless the query is sorted.

TopメソッドのSkip後にメソッドを使用すると、 order by句のLIMITステートメントと同様に機能します。When you use the Top method after the Skip method, it functions like the LIMIT statement of an ORDER BY clause.

適用対象

こちらもご覧ください