Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DataLoadOptions.AssociateWith Method

Filters the objects retrieved for a particular relationship.

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

  Name Description
Public method AssociateWith<T>(Expression<Func<T, Object>>) Filters objects retrieved for a particular relationship.
Public method AssociateWith(LambdaExpression) Filters the objects retrieved for a particular relationship.

Use AssociateWith methods to specify sub-queries to limit the amount of data retrieved.

In the following example, the AssociateWith method limits the Orders retrieved to those that have not been shipped today. Without this approach, all Orders would have been retrieved even though only a subset is desired.

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)
    foreach (Order ord in custObj.Orders)
        Console.WriteLine("\t {0}",ord.OrderDate);
© 2015 Microsoft