延後和立即載入的比較Deferred versus Immediate Loading

當您查詢物件時,實際上只擷取了所要求的物件。When you query for an object, you actually retrieve only the object you requested. 相關物件不會自動擷取一次。The related objects are not automatically fetched at the same time. (如需詳細資訊,請參閱 < 跨關聯性查詢。)因為嘗試存取相關物件會產生對相關物件進行擷取的要求,所以您看不到相關物件尚未載入的事實。(For more information, see Querying Across Relationships.) You cannot see the fact that the related objects are not already loaded, because an attempt to access them produces a request that retrieves them.

例如,您可能要查詢一組特定的訂單,並只是偶爾將電子郵件通知傳送給特定的客戶。For example, you might want to query for a particular set of orders and then only occasionally send an email notification to particular customers. 因此並不需要一開始就擷取每筆訂單的所有客戶資料。You would not necessarily need initially to retrieve all customer data with every order. 您可以使用延後載入,將確切資訊的擷取延後至絕對需要的時候才進行擷取。You can use deferred loading to defer retrieval of extra information until you absolutely have to. 參考下列範例:Consider the following example:

    Northwnd db = new Northwnd(@"northwnd.mdf");

    IQueryable<Order> notificationQuery =
    from ord in db.Orders
 where ord.ShipVia == 3
  select ord;

    foreach (Order ordObj in notificationQuery)
    {
        if (ordObj.Freight > 200)
            SendCustomerNotification(ordObj.Customer);
        ProcessOrder(ordObj);
    }

}
Dim db As New Northwnd("c:\northwnd.mdf")
Dim notificationQuery = _
    From ord In db.Orders _
    Where ord.ShipVia = 3 _
    Select ord

For Each ordObj As Order In notificationQuery
    If ordObj.Freight > 200 Then
        SendCustomerNotification(ordObj.Customer)
        ProcessOrder(ordObj)
    End If

Next

反之亦然。The opposite might also be true. 您的應用程式可能需要同時檢視客戶和訂單資料。You might have an application that has to view customer and order data at the same time. 您知道您需要這兩組資料,You know you need both sets of data. 另外,一旦您取得結果,應用程式就需要每位客戶的訂單資訊。You know your application needs order information for each customer as soon as you get the results. 但您不想要針對每位客戶的訂單分別送出個別查詢,You would not want to submit individual queries for orders for every customer. 您實際想做的是在擷取客戶資料時一起擷取訂單資料。What you really want is to retrieve the order data together with the customers.

Northwnd db = new Northwnd(@"c:\northwnd.mdf");

db.DeferredLoadingEnabled = false;

IQueryable<Customer> custQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

foreach (Customer custObj in custQuery)
{
    foreach (Order ordObj in custObj.Orders)
    {
        ProcessCustomerOrder(ordObj);
    }
}
Dim db As New Northwnd("c:\northwnd.mdf")

db.DeferredLoadingEnabled = False

Dim custQuery = _
    From cust In db.Customers _
    Where cust.City = "London" _
    Select cust

For Each custObj As Customer In custQuery
    For Each ordObj As Order In custObj.Orders
        ProcessCustomerOrder(ordObj)
    Next
Next

您也可以建立叉積查詢,並將資料的所有相對位元都擷取為一個大型投影 (Projection),以利用查詢來聯結 (Join) 客戶和訂單。You can also join customers and orders in a query by forming the cross-product and retrieving all the relative bits of data as one large projection. 但這些結果不是實體 (Entity) But these results are not entities. (如需詳細資訊,請參閱 < LINQ to SQL 物件模型)。(For more information, see The LINQ to SQL Object Model). 實體是具有識別 (Identity) 而且可以修改的物件,而上述結果卻是無法變更和持續 (Persist) 的投影。Entities are objects that have identity and that you can modify, whereas these results would be projections that cannot be changed and persisted. 更糟的是,因為每位客戶會以簡維聯結輸出重複每筆訂單,所以您會擷取到許多多餘的資料。Even worse, you would be retrieving lots of redundant data as each customer repeats for each order in the flattened join output.

您實際需要的是一種可以同時擷取一組相關物件的方式。What you really need is a way to retrieve a set of related objects at the same time. 而這組物件是圖形的明確部分,因此您擷取到的資料就會是您所需要的資料。The set is a delineated section of a graph so that you would never be retrieving more or less than was necessary for your intended use. 基於此目的,LINQ to SQLLINQ to SQL提供DataLoadOptions以立即載入物件模型的區域。For this purpose, LINQ to SQLLINQ to SQL provides DataLoadOptions for immediate loading of a region of your object model. 方法如下:Methods include:

  • LoadWith 方法,可以立即載入與主要目標相關的資料。The LoadWith method, to immediately load data related to the main target.

  • AssociateWith 方法,可以篩選針對特定關聯性 (Relationship) 所擷取的物件。The AssociateWith method, to filter objects retrieved for a particular relationship.

另請參閱See also