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

Definition

Beschränkt die Abfrageergebnisse auf eine bestimmte Anzahl von Elementen.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)

Parameters

count
String

Die Anzahl der Elemente in den Ergebnissen als Zeichenfolge.The number of items in the results as a string.

parameters
ObjectParameter[]

Ein optionaler Satz von Abfrageparametern, die sich beim Analysieren im Gültigkeitsbereich befinden sollten.An optional set of query parameters that should be in scope when parsing.

Returns

ObjectQuery<T>

Eine neue ObjectQuery<T>-Instanz, die der ursprünglichen Instanz entspricht, für die jedoch TOP festgelegt ist.A new ObjectQuery<T> instance that is equivalent to the original instance with TOP applied.

Exceptions

count ist null.count is null.

count ist eine leere Zeichenfolge.count is an empty string.

Examples

Diese Beispiele basieren auf den Microsoft SQL Server Product Samples: Database.These examples are based on the Microsoft SQL Server Product Samples: Database.

In diesem Beispiel wird eine neue ObjectQuery<T> erstellt, die die ersten beiden Ergebnisse der bestehenden Abfrage enthält.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

In diesem Beispiel werden fünf Product Objekte abgerufen, nachdem die ersten drei Objekte im Abfrageergebnis übersprungen wurden, sortiert nach Product.ListPrice.This example gets five Product objects after skipping the first three in the query result, sorted by Product.ListPrice. Top wird anstelle von Limit für Paging verwendet.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

Remarks

Wenn die Abfrage nicht sortiert ist, ist Top nicht deterministisch.Top is nondeterministic unless the query is sorted.

Wenn Sie die Top-Methode nach der Skip-Methode verwenden, funktioniert Sie wie die Limit -Anweisung einer Order by -Klausel.When you use the Top method after the Skip method, it functions like the LIMIT statement of an ORDER BY clause.

Applies to

See also