Export (0) Print
Expand All

How to: Filter at the DataContext Level (LINQ to SQL)

You can filter EntitySets at the DataContext level. Such filters apply to all queries done with that DataContext instance.

In the following example, DataLoadOptions.AssociateWith(LambdaExpression) is used to filter the pre-loaded orders for customers by ShippedDate.

Northwnd db = new Northwnd(@"northwnd.mdf");
// Preload Orders for Customer. 
// One directive per relationship to be preloaded.
DataLoadOptions ds = new DataLoadOptions();
ds.LoadWith<Customer>(c => c.Orders);
    (c => c.Orders.Where(p => p.ShippedDate != DateTime.Today));
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);

Community Additions

© 2015 Microsoft