Export (0) Print
Expand All

ObjectQuery(Of T).Top Method

Limits the query results to a specified number of items.

Namespace:  System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

'Declaration
Public Function Top ( _
	count As String, _
	ParamArray parameters As ObjectParameter() _
) As ObjectQuery(Of T)

Parameters

count
Type: System.String

The number of items in the results as a string.

parameters
Type: System.Data.Objects.ObjectParameter()

An optional set of query parameters that should be in scope when parsing.

Return Value

Type: System.Data.Objects.ObjectQuery(Of T)
A new ObjectQuery(Of T) instance that is equivalent to the original instance with TOP applied.

ExceptionCondition
ArgumentNullException

count is Nothing.

ArgumentException

count is an empty string.

Top is nondeterministic unless the query is sorted.

When you use the Top method after the Skip method, it functions like the LIMIT statement of an ORDER BY clause.

These examples are based on the Adventure Works Sales Model.

This example creates a new ObjectQuery(Of T) that contains the first two results of the existing query.

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

This example gets five Product objects after skipping the first three in the query result, sorted by Product.ListPrice. Top is used instead of LIMIT for paging.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft