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.

  NameDescription
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.
Top

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

Community Additions

ADD
Show:
© 2014 Microsoft