通过


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[]

分析时应位于范围内的可选查询参数集。

返回

与应用 TOP 的原始实例等效的新ObjectQuery<T>实例。

例外

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.ListPriceTop 用于分页,而不是 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 是不确定的,除非对查询进行排序。

在方法之后Skip使用Top该方法时,该方法的工作方式类似于 ORDER BY 子句的 LIMIT 语句。

适用于

另请参阅