ObjectQuery<T>.Top(String, ObjectParameter[]) 方法

定義

將查詢結果限製為指定的項目數目。

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

結果中的項目數量以串形式呈現。

parameters
ObjectParameter[]

一組可選的查詢參數,解析時應該包含在範圍內。

傳回

一個與原始實例相同的新 ObjectQuery<T> 實例,且套用 了 TOP

例外狀況

countnull

count 是空字串。

範例

此範例會建立一個包含現有查詢前兩個結果的新資料 ObjectQuery<T>

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);
}

此範例跳過查詢結果中前三個物件後,Product依 排序 ,獲得五個Product.ListPrice物件。 Top 用於分頁時,會用來取代 LIMIT

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);
}

備註

Top 除非查詢被排序,否則是非確定性的。

當你在方法之後Top使用該Skip方法時,它的運作方式就像 ORDER BY 子句的 LIMIT 陳述。

適用於

另請參閱