static private void ReturnNestedCollectionWithEntityCommand() { //<snippetReturnNestedCollectionWithEntityCommand> using (EntityConnection conn = new EntityConnection("name=AdventureWorksEntities")) { conn.Open(); try { // Create an EntityCommand. using (EntityCommand cmd = conn.CreateCommand()) { // Create a nested query. string esqlQuery = @"Select c.ContactID, c.SalesOrderHeader From AdventureWorksEntities.Contact 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"]); } } } } } catch (EntityException ex) { Console.WriteLine(ex.ToString()); } conn.Close(); } //</snippetReturnNestedCollectionWithEntityCommand> }