방법: 엔터티 형식 개체를 반환하는 쿼리 실행(Entity Framework)

이 항목에서는 엔터티의 컬렉션을 반환하는 쿼리를 실행하는 방법에 대한 예제를 제공합니다. 그런 다음 Contacts의 컬렉션을 반복하여 각 Contact의 첫 번째 이름과 마지막 이름을 표시합니다. . 단일 개체만 반환하려면 쿼리를 기반으로 First, FirstOrDefault, SingleSingleOrDefault 메서드 중 하나를 사용합니다.

또한 동일한 예제에서 다음의 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 LastName = "Zhou"
    Dim query = From contact In context.Contacts Where contact.LastName = LastName

    ' Iterate through the collection of Contact items.
    For Each result As Contact In query
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}", _
                result.FirstName, result.LastName)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    string LastName = "Zhou";
    var query = from contact in context.Contacts where 
                    contact.LastName == LastName select contact;

    // Iterate through the collection of Contact items.
    foreach( var result in query)
    {
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}", 
                result.FirstName, result.LastName);
    }
}

다음은 Entity SQL 예제입니다.

Using context As New AdventureWorksEntities()

    Dim esqlString As String = "SELECT VALUE Contact " & _
        "FROM AdventureWorksEntities.Contacts as Contact where Contact.LastName = @ln"

    Dim query As New ObjectQuery(Of Contact)(esqlString, context, MergeOption.NoTracking)
    query.Parameters.Add(New ObjectParameter("ln", "Zhou"))

    ' Iterate through the collection of Contact items. 
    For Each result As Contact In query
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}", _
                result.FirstName, result.LastName)
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string esqlQuery = @"SELECT VALUE Contact
        FROM AdventureWorksEntities.Contacts as Contact where Contact.LastName = @ln";

    // The following query returns a collection of Contact objects.
    ObjectQuery<Contact> query = new ObjectQuery<Contact>(esqlQuery, context, MergeOption.NoTracking);
    query.Parameters.Add(new ObjectParameter("ln", "Zhou"));

    // Iterate through the collection of Contact items.
    foreach (Contact result in query)
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}",
                result.FirstName, result.LastName);
}

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

Using context As New AdventureWorksEntities()
    Dim query As ObjectQuery(Of Contact) = _
        context.Contacts.Where("it.LastName==@ln",
        New ObjectParameter("ln", "Zhou"))

    ' Iterate through the collection of Contact items.
    For Each result As Contact In query
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}", _
                result.FirstName, result.LastName)
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    ObjectQuery<Contact> query= context.Contacts.Where("it.LastName==@ln", 
        new ObjectParameter("ln", "Zhou"));

    // Iterate through the collection of Contact items.
    foreach (Contact result in query)
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}",
                result.FirstName, result.LastName);
}

참고 항목

작업

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

기타 리소스

고급 데이터 모델 정의(Entity Framework 작업)