This documentation is archived and is not being maintained.

ObjectQuery<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)

public ObjectQuery<T> Top(
	string count,
	params ObjectParameter[] parameters


Type: System.String

The number of items in the results as a string.

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<T>
A new ObjectQuery<T> instance that is equivalent to the original instance with TOP applied.


count is null.


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<T> that contains the first two results of the existing query.

using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
        string queryString =
            @"SELECT VALUE Product FROM AdventureWorksEntities.Product AS Product";

        ObjectQuery<Product> productQuery1 =
            new ObjectQuery<Product>(queryString, advWorksContext, 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);
    catch (EntitySqlException ex)

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 (AdventureWorksEntities advWorksContext =
    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 = advWorksContext.Product
                    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);
    catch (EntitySqlException ex)

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5 SP1