Procedura: eseguire una query che restituisce raccolte annidateHow to: Execute a Query that Returns Nested Collections

In questo argomento viene illustrato come eseguire un comando su un modello concettuale usando un oggetto EntityCommand e come recuperare i risultati della raccolta annidata usando un oggetto EntityDataReader.This shows how to execute a command against a conceptual model by using an EntityCommand object, and how to retrieve the nested collection results by using an EntityDataReader.

Per eseguire il codice in questo esempioTo run the code in this example

  1. Aggiungere il modello Sales di AdventureWorks al progetto e configurare il progetto utilizzerà il Entity FrameworkEntity Framework.Add the AdventureWorks Sales Model to your project and configure your project to use the Entity FrameworkEntity Framework. Per ulteriori informazioni, vedere procedura: utilizzare la procedura guidata Entity Data Model.For more information, see How to: Use the Entity Data Model Wizard.

  2. Nella tabella codici per l'applicazione aggiungere le istruzioni using seguenti (Imports in Visual Basic):In the code page for your application, add the following using statements (Imports in Visual Basic):

    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Data.Common;
    using System.Data;
    using System.IO;
    using System.Data.SqlClient;
    using System.Data.EntityClient;
    using System.Data.Metadata.Edm;
    
    Imports System
    Imports System.Collections.Generic
    Imports System.Collections
    Imports System.Data.Common
    Imports System.Data
    Imports System.IO
    Imports System.Data.SqlClient
    Imports System.Data.EntityClient
    Imports System.Data.Metadata.Edm
    

EsempioExample

Oggetto annidati raccolta è una raccolta che si trova all'interno di un'altra raccolta.A nested collection is a collection that is inside another collection. Nell'esempio seguente vengono recuperate una raccolta di Contacts e le raccolte annidate di SalesOrderHeaders che sono associate a ciascun Contact.The following code retrieves a collection of Contacts and the nested collections of SalesOrderHeaders that are associated with each Contact.

using (EntityConnection conn =
    new EntityConnection("name=AdventureWorksEntities"))
{
    conn.Open();
    // Create an EntityCommand.
    using (EntityCommand cmd = conn.CreateCommand())
    {
        // Create a nested query.
        string esqlQuery =
            @"Select c.ContactID, c.SalesOrderHeaders
        From AdventureWorksEntities.Contacts as c";

        cmd.CommandText = esqlQuery;
        // Execute the command.
        using (EntityDataReader rdr =
            cmd.ExecuteReader(CommandBehavior.SequentialAccess))
        {
            // The result returned by this query contains 
            // ContactID and a nested collection of SalesOrderHeader items.
            // associated with this Contact.
            while (rdr.Read())
            {
                // the first column contains Contact ID.
                Console.WriteLine("Contact ID: {0}", rdr["ContactID"]);

                // The second column contains a collection of SalesOrderHeader 
                // items associated with the Contact.
                DbDataReader nestedReader = rdr.GetDataReader(1);
                while (nestedReader.Read())
                {
                    Console.WriteLine("   SalesOrderID: {0} ", nestedReader["SalesOrderID"]);
                    Console.WriteLine("   OrderDate: {0} ", nestedReader["OrderDate"]);
                }
            }
        }
    }
    conn.Close();
}
Using conn As New EntityConnection("name=AdventureWorksEntities")
    conn.Open()
    ' Create an EntityCommand. 
    Using cmd As EntityCommand = conn.CreateCommand()
        ' Create a nested query. 
        Dim esqlQuery As String = "Select c.ContactID, c.SalesOrderHeaders" & _
            " From AdventureWorksEntities.Contacts as c"

        cmd.CommandText = esqlQuery
        ' Execute the command. 
        Using rdr As EntityDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
            ' The result returned by this query contains 
            ' ContactID and a nested collection of SalesOrderHeader items. 
            ' associated with this Contact. 
            While rdr.Read()
                ' the first column contains Contact ID. 
                Console.WriteLine("Contact ID: {0}", rdr("ContactID"))

                ' The second column contains a collection of SalesOrderHeader 
                ' items associated with the Contact. 
                Dim nestedReader As DbDataReader = rdr.GetDataReader(1)
                While nestedReader.Read()
                    Console.WriteLine(" SalesOrderID: {0} ", nestedReader("SalesOrderID"))
                    Console.WriteLine(" OrderDate: {0} ", nestedReader("OrderDate"))
                End While
            End While
        End Using
    End Using
    conn.Close()
End Using

Vedere ancheSee Also

Provider EntityClient per Entity FrameworkEntityClient Provider for the Entity Framework