방법: 익명 형식의 컬렉션을 반환하는 쿼리 실행(Entity Framework)

이 항목에서는 익명 형식의 인스턴스 컬렉션을 반환하는 쿼리를 실행하는 방법에 대한 예제를 제공합니다. 익명 형식은 개념적 모델에 정의되지 않은 형식입니다. 예를 들어 Entity SQL 쿼리 "SELECT p.ProductID, p.Name FROM AdventureWorksEntities.Products as p"는 두 개의 열(ProductID의 경우 정수, Name의 경우 문자열)이 있는 행이 포함된 익명 형식의 컬렉션을 반환합니다. "SELECT p FROM AdventureWorksEntities.Products as p"에서는 Products 컬렉션을 반환하며 Product 형식은 개념적 모델에서 정의됩니다.

또한 동일한 예제에서 다음의 Entity Framework 쿼리 기술을 각각 사용한 경우도 보여 줍니다.

  • LINQ to Entities

  • ObjectQuery<T>가 포함된 Entity SQL

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

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

예제

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

Using context As New AdventureWorksEntities
    Dim products As ObjectSet(Of Product) = context.Products

    Dim query = _
        From product In products _
        Select New With _
        { _
            .ProductId = product.ProductID, _
            .ProductName = product.Name _
        }

    Console.WriteLine("Product Info:")
    For Each productInfo In query
        Console.WriteLine("Product Id: {0} Product name: {1} ", _
                productInfo.ProductId, productInfo.ProductName)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    var query =
        from product in context.Products
        select new
        {
            ProductId = product.ProductID,
            ProductName = product.Name
        };

    Console.WriteLine("Product Info:");
    foreach (var productInfo in query)
    {
        Console.WriteLine("Product Id: {0} Product name: {1} ",
            productInfo.ProductId, productInfo.ProductName);
    }
}

다음은 Entity SQL 예제입니다.

Using context As New AdventureWorksEntities()
    Dim myQuery As String = "SELECT p.ProductID, p.Name FROM AdventureWorksEntities.Products as p"
    For Each rec As DbDataRecord In New ObjectQuery(Of DbDataRecord)(myQuery, context)
        Console.WriteLine("ID {0}; Name {1}", rec(0), rec(1))
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string myQuery = @"SELECT p.ProductID, p.Name FROM 
        AdventureWorksEntities.Products as p";

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

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

Using context As New AdventureWorksEntities()
    ' Use the Select method to define the projection. 
    Dim query As ObjectQuery(Of DbDataRecord) = context.Products.Select("it.ProductID, it.Name")

    ' Iterate through the collection of data rows. 
    For Each rec As DbDataRecord In query
        Console.WriteLine("ID {0}; Name {1}", rec(0), rec(1))
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Use the Select method to define the projection.
    ObjectQuery<DbDataRecord> query =
        context.Products.Select("it.ProductID, it.Name");

    // Iterate through the collection of data rows.
    foreach (DbDataRecord rec in query)
    {
        Console.WriteLine("ID {0}; Name {1}", rec[0], rec[1]);
    }
}

참고 항목

작업

방법: 엔터티 형식 개체를 반환하는 쿼리 실행(Entity Framework)
방법: 기본 형식의 컬렉션을 반환하는 쿼리 실행(Entity Framework)
방법: 매개 변수가 있는 쿼리 실행(Entity Framework)

개념

쿼리 작성기 메서드(Entity Framework)