作法:關閉延後載入
您可以將 DeferredLoadingEnabled 設定為 false
,以關閉延後載入。 如需詳細資訊,請參閱延後載入與立即載入的比較。
注意
關閉物件追蹤時,也會隱含地關閉延後載入。 如需詳細資訊,請參閱操作說明:擷取資訊並設為唯讀。
範例
下列範例顯示如何將 DeferredLoadingEnabled 設定為 false
,以關閉延後載入。
Northwnd db = new Northwnd(@"c:\northwnd.mdf");
db.DeferredLoadingEnabled = false;
DataLoadOptions ds = new DataLoadOptions();
ds.LoadWith<Customer>(c => c.Orders);
ds.LoadWith<Order>(o => o.OrderDetails);
db.LoadOptions = ds;
var custQuery =
from cust in db.Customers
where cust.City == "London"
select cust;
foreach (Customer custObj in custQuery)
{
Console.WriteLine("Customer ID: {0}", custObj.CustomerID);
foreach (Order ord in custObj.Orders)
{
Console.WriteLine("\tOrder ID: {0}", ord.OrderID);
foreach (OrderDetail detail in ord.OrderDetails)
{
Console.WriteLine("\t\tProduct ID: {0}", detail.ProductID);
}
}
}
Dim db As New Northwnd("c:\northwnd.mdf")
db.DeferredLoadingEnabled = False
Dim ds As New DataLoadOptions()
ds.LoadWith(Function(c As Customer) c.Orders)
ds.LoadWith(Of Order)(Function(o) o.OrderDetails)
db.LoadOptions = ds
Dim custQuery = From cust In db.Customers _
Where cust.City = "London" _
Select cust
For Each custObj In custQuery
Console.WriteLine("Customer ID: {0}", custObj.CustomerID)
For Each ord In custObj.Orders
Console.WriteLine(vbTab & "Order ID: {0}", ord.OrderID)
For Each detail In ord.OrderDetails
Console.WriteLine(vbTab & vbTab & "Product ID: {0}", _
detail.ProductID)
Next
Next
Next