방법: 다형성 쿼리 실행(EntityClient)

이 항목에서는 OFTYPE 연산자를 사용하여 다형성 Entity SQL 쿼리를 실행하는 방법을 보여 줍니다.

이 예제의 코드를 실행하려면

  1. CourseManager 응용 프로그램을 빌드합니다. 자세한 내용과 지침은 Entity Framework 퀵 스타트를 참조하십시오.

  2. 연습: 매핑 상속 - 계층당 하나의 테이블에서 계층당 하나의 테이블 상속 구현 섹션의 지침을 수행하여 EDM을 수정합니다.

예제

OFTYPE 식은 컬렉션의 각 요소에 대해 형식 테스트를 수행하기 위해 실행되는 형식 식을 지정합니다. OFTYPE 식은 해당 형식이나 해당 형식의 하위 형식에 해당하는 요소만 포함하는 지정된 형식의 새 컬렉션을 생성합니다. 예를 들어, ManagerEmployee의 하위 형식인 경우 다음 식은 직원 컬렉션에 있는 관리자만으로 구성된 컬렉션을 생성합니다.

OfType(employees, Manager)

다음 예제에서는 OFTYPE 연산자를 사용하여 People 컬렉션의 Students만으로 구성된 컬렉션을 가져와서 표시합니다.

Using connection As EntityConnection = New EntityConnection("name= SchoolEntities ")
    connection.Open()

    ' Create a query that specifies to 
    ' get a collection of Students
    ' with enrollment dates from
    ' a collection of People.
    Dim esqlQuery As String = "SELECT Student.LastName, " & _
            " Student.EnrollmentDate FROM " & _
            " OFTYPE(SchoolEntities.Person, " & _
            " SchoolModel.Student) AS Student "

    ' Create an EntityCommand and pass the connection object 
    ' and Enity SQL query to its constructor.
    Using command As EntityCommand = New EntityCommand(esqlQuery, connection)
        ' Execute the command.
        Using reader As DbDataReader = command.ExecuteReader(CommandBehavior.SequentialAccess)
            ' Start reading.
            Do While reader.Read
                ' Display student's last name and enrollment date.
                Console.Write(reader.Item("LastName"))
                Console.WriteLine(reader.Item("EnrollmentDate"))
            Loop
        End Using
    End Using
End Using
using (EntityConnection conn = new EntityConnection("name=SchoolEntities"))
{
    conn.Open();
    // Create a query that specifies to 
    // get a collection of only Students
    // with enrollment dates from 
    // a collection of People.
    string esqlQuery = @"SELECT Student.LastName, 
        Student.EnrollmentDate FROM 
        OFTYPE(SchoolEntities.Person, 
        SchoolModel.Student) AS Student";

    using (EntityCommand cmd = new EntityCommand(esqlQuery, conn))
    {
        // Execute the command.
        using (DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
        {
            // Start reading.
            while (rdr.Read())
            {
                // Display student's last name and enrollment date.
                Console.Write(rdr["LastName"] + " ");
                Console.WriteLine(rdr["EnrollmentDate"]);
            }
        }
    }
}

참고 항목

작업

방법: 계층당 하나의 테이블 상속을 사용하여 개체 쿼리 만들기 및 실행(Entity Framework)

개념

Entity SQL 언어

기타 리소스

EntityClient 사용(Entity Framework 작업)
How to: Execute an Entity SQL Query Using EntityCommand