방법: 두 쿼리의 통합 정렬(Entity Framework)

이 항목에서는 두 쿼리의 결과를 하나의 결과 집합으로 결합한 다음 결과 집합을 정렬하는 방법을 보여 줍니다. 다음의 Entity Framework 쿼리 기술을 각각 사용하여 동일한 예제를 보여 줍니다.

  • ObjectQuery<T>가 포함된 Entity SQL

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

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

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

예제

다음은 Entity SQL 을 사용한 예제입니다. Entity SQL 쿼리를 통합하고 정렬하려면 중첩을 사용해야 합니다. Entity SQL 에서 중첩된 쿼리는 괄호로 묶어야 합니다.

Using context As New AdventureWorksEntities()
    Dim esqlQuery As String = "SELECT P2.Name, P2.ListPrice FROM ((SELECT P1.Name, P1.ProductID as Pid, P1.ListPrice " & _
        " FROM AdventureWorksEntities.Products as P1 where P1.Name like 'A%') union all" & _
        " (SELECT P1.Name, P1.ProductID as Pid, P1.ListPrice FROM AdventureWorksEntities.Products as P1" & _
        " WHERE P1.Name like 'B%')) as P2 ORDER BY P2.Name"

    For Each rec As DbDataRecord In New ObjectQuery(Of DbDataRecord)(esqlQuery, context)
        Console.WriteLine("Name: {0}; ListPrice: {1}", rec(0), rec(1))
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    String esqlQuery = @"SELECT P2.Name, P2.ListPrice
        FROM ((SELECT P1.Name, P1.ProductID as Pid, P1.ListPrice 
            FROM AdventureWorksEntities.Products as P1
            where P1.Name like 'A%')
        union all
            (SELECT P1.Name, P1.ProductID as Pid, P1.ListPrice 
            FROM AdventureWorksEntities.Products as P1
            WHERE P1.Name like 'B%')
        ) as P2
        ORDER BY P2.Name";

    foreach (DbDataRecord rec in
        new ObjectQuery<DbDataRecord>(esqlQuery, context))
    {
        Console.WriteLine("Name: {0}; ListPrice: {1}", rec[0], rec[1]);
    }
}

다음은 쿼리 작성기 메서드를 사용한 예제입니다.

Using context As New AdventureWorksEntities()
    Dim query As ObjectQuery(Of DbDataRecord) = _
        context.Products.Select("it.Name, it.ProductID As Pid, it.ListPrice") _
        .Where("it.Name LIKE 'A%'").Union(context.Products.Select("it.Name, it.ProductID As Pid, it.ListPrice") _
                                          .Where("it.Name LIKE 'B%'")).Select("it.Name, it.ListPrice").OrderBy("it.Name")

    For Each rec As DbDataRecord In query
        Console.WriteLine("Name: {0}; ListPrice: {1}", rec(0), rec(1))
    Next
    
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    ObjectQuery<DbDataRecord> query =
        context.Products.Select("it.Name, it.ProductID As Pid, it.ListPrice")
        .Where("it.Name LIKE 'A%'").Union(context.Products
        .Select("it.Name, it.ProductID As Pid, it.ListPrice")
        .Where("it.Name LIKE 'B%'")).Select("it.Name, it.ListPrice").OrderBy("it.Name");

    foreach (DbDataRecord rec in query)
    {
        Console.WriteLine("Name: {0}; ListPrice: {1}", rec[0], rec[1]);
    }
}

다음 예제에서는 LINQ to Entities를 사용하여 두 쿼리의 통합을 정렬하는 방법을 보여 줍니다.

Using context As New AdventureWorksEntities()
    Dim query = (From a In context.Products Where a.Name.StartsWith("A") _
                 Select (New With {a.Name, .pid = a.ProductID, a.ListPrice})). _
             Union( _
                 From b In context.Products Where b.Name.StartsWith("B") _
                 Select (New With {b.Name, .pid = b.ProductID, b.ListPrice})). _
             Select(Function(c) New With {c.Name, c.ListPrice}).OrderBy(Function(d) d.Name)


    For Each result In query
        Console.WriteLine(result.Name)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    var query = (from a in context.Products
             where a.Name.StartsWith("A")
             select new { a.Name, pid = a.ProductID, a.ListPrice })
            .Union
            (from b in context.Products
             where b.Name.StartsWith("B")
             select new { b.Name, pid = b.ProductID, b.ListPrice })
            .Select(c => new { c.Name, c.ListPrice }).OrderBy(d => d.Name);

    foreach (var result in query)
    {
        Console.WriteLine(result.Name);
    }
}

참고 항목

개념

개념적 모델 쿼리(Entity Framework)