Share via


Procedura: eseguire una query che restituisce oggetti di tipo entità (Entity Framework)

In questo argomento vengono forniti alcuni esempi di esecuzione di una query che restituisce una raccolta di entità. Viene quindi esaminata l'intera raccolta di oggetti Contacts e vengono visualizzati il nome e il cognome per ogni oggetto Contact. . Per restituire un singolo oggetto, specificare uno dei metodi seguenti nella query: First, FirstOrDefault, Single, SingleOrDefault.

Viene illustrato lo stesso esempio con ognuna delle tecnologie di query Entity Framework seguenti:

  • LINQ to Entities

  • Entity SQL con ObjectQuery<T>

  • Metodi del generatore di query di ObjectQuery<T>

L'esempio incluso in questo argomento è basato sul modello Sales di AdventureWorks. Per eseguire il codice incluso in questo argomento, è necessario avere già aggiunto il modello Sales di AdventureWorks al progetto e avere configurato il progetto per l'utilizzo di Entity Framework. Per ulteriori informazioni, vedere Procedura: utilizzare la Procedura guidata Entity Data Model (Entity Framework) o Procedura: configurare manualmente un progetto di Entity Framework e Procedura: definire manualmente un modello EDM (Entity Framework).

Esempio

Di seguito viene fornito un esempio di utilizzo di 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);
    }
}

Di seguito viene fornito un esempio di utilizzo di 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);
}

Di seguito viene fornito un esempio del metodo del generatore di query.

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

Vedere anche

Attività

Procedura: eseguire una query che restituisce una raccolta di tipi anonimi (Entity Framework)
Procedura: eseguire una query che restituisce una raccolta di tipi primitivi (Entity Framework)
Procedura: eseguire una query con parametri (Entity Framework)

Altre risorse

Definizione di modelli di dati avanzati (attività di Entity Framework)