방법: 쿼리 결과 페이징(Entity Framework)

이 항목에서는 쿼리 결과를 페이징하는 방법을 보여 줍니다. 이 예제에서는 Product.ListPrice를 기준으로 정렬된 쿼리 결과에서 처음 세 개를 건너뛰고 다섯 개의 Product 개체를 가져옵니다. 다음의 Entity Framework 쿼리 기술을 각각 사용하여 동일한 예제를 보여 줍니다.

  • LINQ to Entities

  • ObjectQuery<T>를 사용한 Entity SQL

  • ObjectQuery<T>의 쿼리 작성기 메서드

이 항목의 예제는 Adventure Works Sales 모델을 기반으로 합니다. 이 예제의 코드를 실행하려면 프로젝트에 AdventureWorks Sales 모델을 추가하고 엔터티 프레임워크를 사용하도록 프로젝트를 구성해야 합니다. 이렇게 하려면 방법: Entity Framework 프로젝트 수동 구성방법: 엔터티 데이터 모델 수동 정의(Entity Framework)의 절차를 수행합니다. 엔터티 데이터 모델 마법사를 사용하여 AdventureWorks Sales 모델을 정의할 수도 있습니다. 자세한 내용은 방법: 엔터티 데이터 모델 마법사 사용(Entity Framework)을 참조하십시오.

예제

다음은 LINQ to Entities 예제입니다.

Using AWEntities As New AdventureWorksEntities
    'LINQ to Entities only supports Skip on ordered collections.
    Dim products As IOrderedQueryable(Of Product) = _
            AWEntities.Product.OrderBy(Function(p) p.ListPrice)

    Dim allButFirst3Products As IQueryable(Of Product) = products.Skip(3)

    Console.WriteLine("All but first 3 products:")
    For Each product As Product In allButFirst3Products
        Console.WriteLine("Name: {0} \t ID: {1}", _
                product.Name, _
                product.ProductID)
    Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    // LINQ to Entities only supports Skip on ordered collections.
    IOrderedQueryable<Product> products = AWEntities.Product
            .OrderBy(p => p.ListPrice);

    IQueryable<Product> allButFirst3Products = products.Skip(3);

    Console.WriteLine("All but first 3 products:");
    foreach (Product product in allButFirst3Products)
    {
        Console.WriteLine("Name: {0} \t ID: {1}",
            product.Name,
            product.ProductID);
    }
}

다음은 Entity SQL 예제입니다.

Using advWorksContext As New AdventureWorksEntities()
    ' Define the parameters used to define the "page" of returned data.
    Try
        ' Create a query that takes two parameters.
        Dim esqlQuery As String = "SELECT VALUE product FROM " & _
                  " AdventureWorksEntities.Product AS product " & _
                  " order by product.ListPrice SKIP @skip LIMIT @limit"

        Dim productQuery As New ObjectQuery(Of Product)(esqlQuery, advWorksContext)
        ' Add parameters to the collection.
        productQuery.Parameters.Add(New ObjectParameter("skip", 3))
        productQuery.Parameters.Add(New ObjectParameter("limit", 5))


        ' Iterate through the page of Product items.
        For Each result As Product In productQuery
            Console.WriteLine("ID: {0} Name: {1}", _
            result.ProductID, result.Name)
        Next
    Catch ex As EntityException
        Console.WriteLine(ex.ToString())
    Catch ex As InvalidOperationException
        Console.WriteLine(ex.ToString())
    End Try
End Using
using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
{
    try
    {
        // Create a query that takes two parameters.
        string queryString =
            @"SELECT VALUE product FROM 
              AdventureWorksEntities.Product AS product 
              order by product.ListPrice SKIP @skip LIMIT @limit";

        ObjectQuery<Product> productQuery =
            new ObjectQuery<Product>(queryString, advWorksContext);

        // Add parameters to the collection.
        productQuery.Parameters.Add(new ObjectParameter("skip", 3));
        productQuery.Parameters.Add(new ObjectParameter("limit", 5));

        // Iterate through the collection of Contact items.
        foreach (Product result in productQuery)
            Console.WriteLine("ID: {0}; Name: {1}",
            result.ProductID, result.Name);
    }
    catch (EntityException ex)
    {
        Console.WriteLine(ex.ToString());
    }
    catch (InvalidOperationException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

다음은 쿼리 작성기 방법을 사용한 예제입니다.

Using advWorksContext As New AdventureWorksEntities()
    ' Define the parameters used to define the "page" of returned data.
    Dim skipValue As Integer = 3
    Dim limitValue As Integer = 5

    Try
        ' 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) = advWorksContext.Product _
            .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
    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString())
    End Try
End Using
using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
{
    // Define the parameters used to define the "page" of returned data.
    int skipValue = 3;
    int limitValue = 5;

    try
    {
        // 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
            .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);
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

참고 항목

참조

SKIP(Entity SQL)
LIMIT(Entity SQL)
Skip
Top

개념

페이징(Entity SQL)
Entity SQL 언어

기타 리소스

EntityClient 사용(Entity Framework 작업)
How to: Execute an Entity SQL Query Using EntityCommand