방법: 매개 변수가 있는 쿼리 실행(Entity Framework)

이 항목에서는 ObjectQuery를 사용하여 매개 변수가 있는 Entity SQL 쿼리를 실행하는 방법을 보여 줍니다. 이 예제에서는 두 매개 변수를 ObjectQuery에 전달하고, 쿼리를 실행한 다음 Contact 항목 컬렉션을 반복합니다. 또한 동일한 예제에서 다음의 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 FirstName = "Frances"
    Dim LastName = "Adams"

    Dim contactQuery = From contact In context.Contacts _
        Where contact.LastName = LastName AndAlso contact.FirstName = FirstName _
            Select contact


    ' Iterate through the results of the parameterized query.
    For Each result In contactQuery
        Console.WriteLine("{0} {1}", result.FirstName, result.LastName)
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string FirstName = "Frances";
    string LastName = "Adams";


    var contactQuery = from contact in context.Contacts
                       where contact.LastName == LastName && contact.FirstName == FirstName
                       select contact;

    // Iterate through the results of the parameterized query.
    foreach (var result in contactQuery)
    {
        Console.WriteLine("{0} {1} ", result.FirstName, result.LastName);
    }
}

Entity SQL 예제는 다음과 같습니다.

Using context As New AdventureWorksEntities()
    ' Create a query that takes two parameters. 
    Dim queryString As String = "SELECT VALUE Contact FROM AdventureWorksEntities.Contacts " & _
        " AS Contact WHERE Contact.LastName = @ln AND Contact.FirstName = @fn"

    Dim contactQuery As New ObjectQuery(Of Contact)(queryString, context)

    ' Add parameters to the collection. 
    contactQuery.Parameters.Add(New ObjectParameter("ln", "Adams"))
    contactQuery.Parameters.Add(New ObjectParameter("fn", "Frances"))

    ' Iterate through the collection of Contact items. 
    For Each result As Contact In contactQuery
        Console.WriteLine("Last Name: {0}; First Name: {1}", result.LastName, result.FirstName)
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Create a query that takes two parameters.
    string queryString =
        @"SELECT VALUE Contact FROM AdventureWorksEntities.Contacts 
                AS Contact WHERE Contact.LastName = @ln AND
                Contact.FirstName = @fn";

    ObjectQuery<Contact> contactQuery =
        new ObjectQuery<Contact>(queryString, context);

    // Add parameters to the collection.
    contactQuery.Parameters.Add(new ObjectParameter("ln", "Adams"));
    contactQuery.Parameters.Add(new ObjectParameter("fn", "Frances"));

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

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

Dim firstName As String = "Frances"
Dim lastName As String = "Adams"

Using context As New AdventureWorksEntities()
    ' Get the contacts with the specified name. 
    Dim contactQuery As ObjectQuery(Of Contact) = context.Contacts.Where("it.LastName = @ln AND it.FirstName = @fn", _
                                                     New ObjectParameter("ln", lastName), New ObjectParameter("fn", firstName))

    ' Iterate through the collection of Contact items. 
    For Each result As Contact In contactQuery
        Console.WriteLine("Last Name: {0}; First Name: {1}", result.LastName, result.FirstName)
    Next
End Using
string firstName = @"Frances";
string lastName = @"Adams";

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Get the contacts with the specified name.
    ObjectQuery<Contact> contactQuery = context.Contacts
        .Where("it.LastName = @ln AND it.FirstName = @fn",
        new ObjectParameter("ln", lastName),
        new ObjectParameter("fn", firstName));

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

참고 항목

작업

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

개념

Entity SQL 언어