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

정의

지정된 개수의 항목으로 쿼리 결과를 제한합니다.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)

매개 변수

count
String String String String

문자열 형태의 결과 항목 개수입니다.The number of items in the results as a string.

parameters
ObjectParameter[]

구문 분석 범위에 포함되어야 하는 쿼리 매개 변수의 선택적 집합입니다.An optional set of query parameters that should be in scope when parsing.

반환

원래 인스턴스에 TOP이 적용된 것과 동일한 새 ObjectQuery<T> 인스턴스입니다.A new ObjectQuery<T> instance that is equivalent to the original instance with TOP applied.

예외

count이 빈 문자열인 경우count is an empty string.

예제

이러한 예제를 기반으로 합니다 AdventureWorks Sales 모델합니다.These examples are based on the AdventureWorks Sales Model.

이 예제에서는 기존 쿼리의 처음 두 결과를 포함하는 새 ObjectQuery<T>를 만듭니다.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

이 예제에서는 5 개를 가져옵니다 Product 별로 정렬 된 쿼리 결과에서 처음 세 개를 건너뛰고 개체 Product.ListPrice합니다.This example gets five Product objects after skipping the first three in the query result, sorted by Product.ListPrice. Top 대신 사용 됩니다 제한 페이징에 대 한 합니다.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

설명

쿼리가 정렬되지 않는 한 Top은 비결정적입니다.Top is nondeterministic unless the query is sorted.

사용 하는 경우는 Top 메서드를 Skip 처럼 작동 하는 메서드를 합니다 제한 설명은 ORDER BY 절.When you use the Top method after the Skip method, it functions like the LIMIT statement of an ORDER BY clause.

적용 대상

추가 정보