方法: 2 つのクエリの結合を並べ替える (Entity Framework)

このトピックでは、2 つのクエリの結果を単一の結果セットに結合し、その結果セットを並べ替える方法について説明します。 The same example is shown using each of the following Entity Framework query technologies:

  • Entity SQL と ObjectQuery<T>

  • ObjectQuery<T> のクエリ ビルダー メソッド

  • ObjectQuery<T> のクエリ ビルダー メソッド

このトピックの例には、Adventure Works Sales Model が使用されています。このトピックのコードを実行するには、あらかじめプロジェクトに Adventure Works Sales Model を追加し、Entity Framework を使用するようにプロジェクトを構成しておく必要があります。詳細については、「Entity Data Model ウィザードを使用する方法 (Entity Framework)」、または「Entity Framework プロジェクトを手動で構成する方法」、および「Entity Data Model を手動で定義する方法 (Entity Framework)」を参照してください。

The following is the Entity SQL example. 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 を使用して 2 つのクエリの結合を並べ替える方法を次の例に示します。

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)