Condividi tramite


Procedura: eseguire una query polimorfica (EntityClient)

In questo argomento viene illustrato come eseguire una query Entity SQL polimorfica utilizzando l'operatore OFTYPE.

Per eseguire il codice in questo esempio

  1. Compilare l'applicazione CourseManager. Per ulteriori informazioni e istruzioni, vedere la Guida rapida di Entity Framework.

  2. Modificarne il modello EDM completando le istruzioni nella sezione Implementazione dell'ereditarietà tabella per gerarchia in Procedura dettagliata: eseguire il mapping dell'ereditarietà tabella per gerarchia.

Esempio

Un'espressione OFTYPE specifica un'espressione del tipo utilizzata per eseguire un test del tipo rispetto a ogni elemento di un insieme. L'espressione OFTYPE produce un nuovo insieme del tipo specificato, contenente solo gli elementi equivalenti o al tipo o al relativo sottotipo. Considerando, ad esempio, Manager come sottotipo di Employee, l'espressione seguente produce un insieme composto solo da dirigenti (manager) a partire da un insieme di dipendenti (employee):

OfType(employees, Manager)

Nell'esempio seguente viene utilizzato un operatore OFTYPE per ottenere e visualizzare un insieme di soli oggetti Students da un insieme di oggetti People.

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"]);
            }
        }
    }
}

Vedere anche

Attività

Procedura: creare ed eseguire query di oggetto utilizzando l'ereditarietà tabella per gerarchia (Entity Framework)

Concetti

Linguaggio Entity SQL

Altre risorse

Utilizzo di EntityClient (attività di Entity Framework)
How to: Execute an Entity SQL Query Using EntityCommand