ALinq

Linq To Access, MS SQL, SQLite, MySQL, Oracle, Firebird, DB2, PostgreSQL ......

ALinq Document > Programming Guide > Querying the Database

Filter at the DataContext Level

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

Example

In the following example, DataLoadOptions.AssociateWith(LambdaExpression) is used to filter the pre-loaded orders for customers by ShippedDate.
var db = new SQLiteNorthwind(@"C:/northwind.db") { Log = Console.Out };
// Preload Orders for Customer.
// One directive per relationship to be preloaded.
var ds = new ALinq.DataLoadOptions();
ds.LoadWith<Customer>(c => c.Orders);
ds.AssociateWith<Customer>
    (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);
        }
    }
}
Dim db = New SQLiteNorthwind("C:/northwind.db") _
         With {.Log = Console.Out}

' Preload Orders for Customer.
' One directive per relationship to be preloaded.
Dim ds = New ALinq.DataLoadOptions()
ds.LoadWith(Of Customer)(Function(c) c.Orders)
ds.AssociateWith(Of Customer) _
        (Function(c) c.Orders.Where(Function(p) p.ShippedDate <> DateTime.Today))
db.LoadOptions = ds

Dim custQuery = _
        From cust In db.Customers() _
        Where cust.City = "London" _
        Select cust

For Each custObj As Customer In custQuery
    Console.WriteLine("Customer ID: {0}", custObj.CustomerID)
    For Each ord As Order In custObj.Orders
        Console.WriteLine(vbTab + "Order ID: {0}", ord.OrderID)
        For Each detail As OrderDetail In ord.OrderDetails
            Console.WriteLine(vbTab + vbTab + "Product ID: {0}", detail.ProductID)
        Next
    Next
Next