使用 AssociateWith 方法可指定用来限制检索到的数据量的子查询。
示例
在下面的示例中,AssociateWith 方法将检索到的 Orders 限制为今天尚未发货的那些订单。 如果不使用这种方式,则即使只需要一部分订单,也会检索到所有 Orders。
Dim db As New Northwnd("c:\northwnd.mdf")
Dim dlo As DataLoadOptions = New DataLoadOptions()
dlo.AssociateWith(Of Customer)(Function(c As Customer) _
c.Orders.Where(Function(p) p.ShippedDate <> DateTime.Today))
db.LoadOptions = dlo
Dim custOrderQuery = _
From cust In db.Customers _
Where cust.City = "London" _
Select cust
For Each custObj In custOrderQuery
Console.WriteLine(custObj.CustomerID)
For Each ord In custObj.Orders
Console.WriteLine("{0}{1}", vbTab, ord.OrderDate)
Next
Next
Northwnd db = new Northwnd(@"c:\northwnd.mdf");
DataLoadOptions dlo = new DataLoadOptions();
dlo.AssociateWith<Customer>(c => c.Orders.Where(p => p.ShippedDate != DateTime.Today));
db.LoadOptions = dlo;
var custOrderQuery =
from cust in db.Customers
where cust.City == "London"
select cust;
foreach (Customer custObj in custOrderQuery)
{
Console.WriteLine(custObj.CustomerID);
foreach (Order ord in custObj.Orders)
{
Console.WriteLine("\t {0}",ord.OrderDate);
}
}