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

Definisi

Membatasi hasil kueri ke jumlah item tertentu.

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)

Parameter

count
String

Jumlah item dalam hasil sebagai string.

parameters
ObjectParameter[]

Sekumpulan parameter kueri opsional yang harus berada dalam cakupan saat mengurai.

Mengembalikan

Instans baru ObjectQuery<T> yang setara dengan instans asli dengan TOP diterapkan.

Pengecualian

countadalah null.

count adalah string kosong.

Contoh

Contoh ini membuat baru ObjectQuery<T> yang berisi dua hasil pertama dari kueri yang sudah ada.

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

Contoh ini mendapatkan lima Product objek setelah melompati tiga objek pertama dalam hasil kueri, diurutkan menurut Product.ListPrice. Top digunakan alih-alih LIMIT untuk penomor.

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

Keterangan

Top nondeterministik kecuali kueri diurutkan.

Saat Anda menggunakan Top metode setelah Skip metode , metode ini berfungsi seperti pernyataan LIMIT dari klausa ORDER BY .

Berlaku untuk

Lihat juga