ObjectQuery<T>.Include(String) 方法

定義

指定要包含在查詢結果中的相關物件。Specifies the related objects to include in the query results.

public:
 System::Data::Objects::ObjectQuery<T> ^ Include(System::String ^ path);
public System.Data.Objects.ObjectQuery<T> Include (string path);
member this.Include : string -> System.Data.Objects.ObjectQuery<'T>
Public Function Include (path As String) As ObjectQuery(Of T)

參數

path
String

要在查詢結果中傳回之相關物件的點分隔清單。Dot-separated list of related objects to return in the query results.

傳回

ObjectQuery<T>

含有已定義之查詢路徑的新 ObjectQuery<T>A new ObjectQuery<T> with the defined query path.

例外狀況

pathnullpath is null.

pathemptypath is empty.

範例

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Define an object query with a path that returns
    // orders and items for a specific contact.
    Contact contact =
        context.Contacts.Include("SalesOrderHeaders.SalesOrderDetails")
        .FirstOrDefault();

    // Execute the query and display information for each item
    // in the orders that belong to the first contact.
    foreach (SalesOrderHeader order in contact
        .SalesOrderHeaders)
    {
        Console.WriteLine(String.Format("PO Number: {0}",
            order.PurchaseOrderNumber));
        Console.WriteLine(String.Format("Order Date: {0}",
            order.OrderDate.ToString()));
        Console.WriteLine("Order items:");
        foreach (SalesOrderDetail item in order.SalesOrderDetails)
        {
            Console.WriteLine(String.Format("Product: {0} "
                + "Quantity: {1}", item.ProductID.ToString(),
                item.OrderQty.ToString()));
        }
    }
}
Using context As New AdventureWorksEntities()
    ' Define an object query with a path that returns 
    ' orders and items for a specific contact. 
    Dim contact As Contact = context.Contacts.Include("SalesOrderHeaders.SalesOrderDetails").FirstOrDefault()

    ' Execute the query and display information for each item 
    ' in the orders that belong to the first contact. 
    For Each order As SalesOrderHeader In contact.SalesOrderHeaders
        Console.WriteLine(String.Format("PO Number: {0}", order.PurchaseOrderNumber))
        Console.WriteLine(String.Format("Order Date: {0}", order.OrderDate.ToString()))
        Console.WriteLine("Order items:")
        For Each item As SalesOrderDetail In order.SalesOrderDetails
            Console.WriteLine(String.Format("Product: {0} Quantity: {1}", _
                                              item.ProductID.ToString(), item.OrderQty.ToString()))
        Next
    Next
End Using

備註

查詢路徑可以搭配 Entity SQL 和 LINQ 查詢使用。Query paths can be used with Entity SQL and LINQ queries.

路徑是全部包含的。Paths are all-inclusive. 例如,如果包含呼叫指出,則 Include("Orders.OrderLines") 不僅 OrderLines 包含在內,也會包含在內 OrdersFor example, if an include call indicates Include("Orders.OrderLines"), not only will OrderLines be included, but also Orders. 如需詳細資訊,請參閱 載入相關物件For more information, see Loading Related Objects.

當您呼叫 Include 方法時,此查詢路徑只適用於 ObjectQuery<T> 的傳回執行個體。When you call the Include method, the query path is only valid on the returned instance of the ObjectQuery<T>. 其他 ObjectQuery<T> 執行個體和物件內容本身則不會受到影響。Other instances of ObjectQuery<T> and the object context itself are not affected.

由於 Include 方法會傳回查詢物件,因此您可以針對 ObjectQuery<T> 多次呼叫這個方法來指定查詢的多個路徑,如下列範例所示:Because the Include method returns the query object, you can call this method multiple times on an ObjectQuery<T> to specify multiple paths for the query, as in the following example:

// Create a SalesOrderHeader query with two query paths,
// one that returns order items and a second that returns the
// billing and shipping addresses for each order.
ObjectQuery<SalesOrderHeader> query =
    context.SalesOrderHeaders.Include("SalesOrderDetails").Include("Address");
' Create a SalesOrderHeader query with two query paths, 
' one that returns order items and a second that returns the 
' billing and shipping addresses for each order. 
Dim query As ObjectQuery(Of SalesOrderHeader) = context.SalesOrderHeaders.Include("SalesOrderDetails").Include("Address")

適用於