Esempi di sintassi di espressione di query: esplorazione di relazioniQuery Expression Syntax Examples: Navigating Relationships

Le proprietà di navigazione in Entity FrameworkEntity Framework sono proprietà di collegamento usate per individuare le entità finali di un'associazione.Navigation properties in the Entity FrameworkEntity Framework are shortcut properties used to locate the entities at the ends of an association. Le proprietà di navigazione consentono a un utente di spostarsi da un'entità a un'altra o da un entità alle entità correlate tramite un set di associazioni.Navigation properties allow a user to navigate from one entity to another, or from one entity to related entities through an association set. In questo argomento sono inclusi alcuni esempi nella sintassi delle espressioni di query in cui viene illustrato come spostarsi tra relazioni tramite proprietà di navigazione in query LINQ to EntitiesLINQ to Entities.This topic provides examples in query expression syntax of how to navigate relationships through navigation properties in LINQ to EntitiesLINQ to Entities queries.

Il modello Sales di AdventureWorks usato in questi esempi è compilato in base alle tabelle Contact, Address, Product, SalesOrderHeader e SalesOrderDetail del database di esempio AdventureWorks.The AdventureWorks Sales Model used in these examples is built from the Contact, Address, Product, SalesOrderHeader, and SalesOrderDetail tables in the AdventureWorks sample database.

Gli esempi in questo argomento usano seguenti using / Imports istruzioni:The examples in this topic use the following using/Imports statements:

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Globalization;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Data.Common;
Option Explicit On
Option Strict On
Imports System.Data.Objects
Imports System.Globalization

EsempioExample

Nell'esempio seguente viene usato il metodo Select per ottenere tutti gli ID contatto e la somma del totale dovuto per ogni contatto il cui cognome è "Zhou".The following example uses the Select method to get all the contact IDs and the sum of the total due for each contact whose last name is "Zhou". La proprietà di navigazione Contact.SalesOrderHeader viene usata per ottenere la raccolta di oggetti SalesOrderHeader per ciascun contatto.The Contact.SalesOrderHeader navigation property is used to get the collection of SalesOrderHeader objects for each contact. Il metodo Sum usa la proprietà di navigazione Contact.SalesOrderHeader per sommare il totale dovuto per tutti gli ordini per ciascun contatto.The Sum method uses the Contact.SalesOrderHeader navigation property to sum the total due of all the orders for each contact.

string lastName = "Zhou";
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Contact> contacts = context.Contacts;

    var ordersQuery = from contact in contacts
                      where contact.LastName == lastName
                      select new
                      {
                          ContactID = contact.ContactID,
                          Total = contact.SalesOrderHeaders.Sum(o => o.TotalDue)
                      };

    foreach (var contact in ordersQuery)
    {
        Console.WriteLine("Contact ID: {0} Orders total: {1}", contact.ContactID, contact.Total);
    }
}
Dim lastName = "Zhou"
Using context As New AdventureWorksEntities
    Dim contacts As ObjectSet(Of Contact) = context.Contacts

    Dim ordersQuery = From contact In contacts _
        Where contact.LastName = lastName _
        Select New With _
                {.ContactID = contact.ContactID, _
                .Total = contact.SalesOrderHeaders.Sum(Function(o) o.TotalDue)}

    For Each order In ordersQuery
        Console.WriteLine("Contact ID: {0} Orders total: {1}", order.ContactID, order.Total)
    Next
End Using

EsempioExample

Nell'esempio seguente vengono ottenuti tutti gli ordini dei contatti il cui cognome è "Zhou".The following example gets all the orders of the contacts whose last name is "Zhou". La proprietà di navigazione Contact.SalesOrderHeader viene usata per ottenere la raccolta di oggetti SalesOrderHeader per ciascun contatto.The Contact.SalesOrderHeader navigation property is used to get the collection of SalesOrderHeader objects for each contact. Il nome e gli ordini del contatto vengono restituiti in un tipo anonimo.The contact's name and orders are returned in an anonymous type.

string lastName = "Zhou";
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Contact> contacts = context.Contacts;

    var ordersQuery = from contact in contacts
                      where contact.LastName == lastName
                      select new { LastName = contact.LastName, Orders = contact.SalesOrderHeaders };

    foreach (var order in ordersQuery)
    {
        Console.WriteLine("Name: {0}", order.LastName);
        foreach (SalesOrderHeader orderInfo in order.Orders)
        {
            Console.WriteLine("Order ID: {0}, Order date: {1}, Total Due: {2}",
                orderInfo.SalesOrderID, orderInfo.OrderDate, orderInfo.TotalDue);
        }
        Console.WriteLine("");
    }
}
Dim lastName = "Zhou"
Using context As New AdventureWorksEntities
    Dim contacts As ObjectSet(Of Contact) = context.Contacts

    Dim ordersQuery = From contact In contacts _
        Where contact.LastName = lastName _
        Select New With _
                {.LastName = contact.LastName, _
                 .Orders = contact.SalesOrderHeaders}

    For Each order In ordersQuery
        Console.WriteLine("Name: {0}", order.LastName)
        For Each orderInfo In order.Orders

            Console.WriteLine("Order ID: {0}, Order date: {1}, Total Due: {2}", _
                    orderInfo.SalesOrderID, orderInfo.OrderDate, orderInfo.TotalDue)
        Next

        Console.WriteLine("")
    Next
End Using

EsempioExample

Nell'esempio seguente vengono usate le proprietà di navigazione SalesOrderHeader.Address e SalesOrderHeader.Contact per ottenere la raccolta di oggetti Address e Contact associati a ogni ordine.The following example uses the SalesOrderHeader.Address and SalesOrderHeader.Contact navigation properties get the collection of Address and Contact objects associated with each order. Il cognome del contatto, l'indirizzo, il numero dell'ordine di vendita e il totale dovuto per ogni ordine alla città di Seattle vengono restituiti in un tipo anonimo.The last name of the contact, the street address, the sales order number, and the total due for each order to the city of Seattle are returned in an anonymous type.

string city = "Seattle";
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<SalesOrderHeader> orders = context.SalesOrderHeaders;

    var ordersQuery = from order in orders
                      where order.Address.City == city
                      select new
                      {
                          ContactLastName = order.Contact.LastName,
                          ContactFirstName = order.Contact.FirstName,
                          StreetAddress = order.Address.AddressLine1,
                          OrderNumber = order.SalesOrderNumber,
                          TotalDue = order.TotalDue
                      };

    foreach (var orderInfo in ordersQuery)
    {
        Console.WriteLine("Name: {0}, {1}", orderInfo.ContactLastName, orderInfo.ContactFirstName);
        Console.WriteLine("Street address: {0}", orderInfo.StreetAddress);
        Console.WriteLine("Order number: {0}", orderInfo.OrderNumber);
        Console.WriteLine("Total Due: {0}", orderInfo.TotalDue);
        Console.WriteLine("");
    }
}
Dim city = "Seattle"
Using context As New AdventureWorksEntities

    Dim orders As ObjectSet(Of SalesOrderHeader) = context.SalesOrderHeaders

    Dim ordersQuery = From order In orders _
             Where order.Address.City = city _
             Select New With { _
                            .ContactLastName = order.Contact.LastName, _
                            .ContactFirstName = order.Contact.FirstName, _
                            .StreetAddress = order.Address.AddressLine1, _
                            .OrderNumber = order.SalesOrderNumber, _
                            .TotalDue = order.TotalDue}

    For Each orderInfo In ordersQuery
        Console.WriteLine("Name: {0}, {1}", orderInfo.ContactLastName, orderInfo.ContactFirstName)
        Console.WriteLine("Street address: {0}", orderInfo.StreetAddress)
        Console.WriteLine("Order number: {0}", orderInfo.OrderNumber)
        Console.WriteLine("Total Due: {0}", orderInfo.TotalDue)
        Console.WriteLine("")
    Next

End Using

EsempioExample

Nell'esempio seguente viene usato il metodo Where per individuare gli ordini effettuati dopo l'1 dicembre 2003, quindi viene usata la proprietà di navigazione order.SalesOrderDetail per ottenere i dettagli relativi a ogni ordine.The following example uses the Where method to find orders that were made after December 1, 2003, and then uses the order.SalesOrderDetail navigation property to get the details for each order.

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    IQueryable<SalesOrderHeader> query =
        from order in context.SalesOrderHeaders
        where order.OrderDate >= new DateTime(2003, 12, 1)
        select order;


    Console.WriteLine("Orders that were made after December 1, 2003:");
    foreach (SalesOrderHeader order in query)
    {
        Console.WriteLine("OrderID {0} Order date: {1:d} ",
            order.SalesOrderID, order.OrderDate);
        foreach (SalesOrderDetail orderDetail in order.SalesOrderDetails)
        {
            Console.WriteLine("  Product ID: {0} Unit Price {1}",
                orderDetail.ProductID, orderDetail.UnitPrice);
        }
    }
}
Using context As New AdventureWorksEntities
    Dim orders As ObjectSet(Of SalesOrderHeader) = context.SalesOrderHeaders

    Dim query = _
        From order In orders _
        Where order.OrderDate >= New DateTime(2003, 12, 1) _
        Select order

    Console.WriteLine("Orders that were made after December 1, 2003:")
    For Each order In query
        Console.WriteLine("OrderID {0} Order date: {1:d} ", _
                order.SalesOrderID, order.OrderDate)
        For Each orderDetail In order.SalesOrderDetails
            Console.WriteLine("  Product ID: {0} Unit Price {1}", _
                orderDetail.ProductID, orderDetail.UnitPrice)
        Next
    Next
End Using

Vedere ancheSee Also

Query in LINQ to EntitiesQueries in LINQ to Entities