共用方式為


ObjectQuery<T>.Include(String) 方法

定義

指定要包含在查詢結果中的相關物件。

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

要在查詢結果中傳回之相關物件的點分隔清單。

傳回

含有已定義之查詢路徑的新 ObjectQuery<T>

例外狀況

pathnull

pathempty

範例

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

備註

查詢路徑可以搭配 Entity SQL 和 LINQ 查詢使用。

路徑是全部包含的。 例如,如果 include 呼叫指出 Include("Orders.OrderLines"),不僅會 OrderLines 包含 ,也會包含 Orders。 如需詳細資訊,請參閱載入相關物件

當您呼叫 Include 方法時,此查詢路徑只適用於 ObjectQuery<T> 的傳回執行個體。 其他 ObjectQuery<T> 執行個體和物件內容本身則不會受到影響。

由於 Include 方法會傳回查詢物件,因此您可以針對 ObjectQuery<T> 多次呼叫這個方法來指定查詢的多個路徑,如下列範例所示:

// 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");

適用於

另請參閱