Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5

  Switch on low bandwidth view
Members FilterMembers Filter
Frameworks FilterFrameworks Filter
.NET Framework Class Library
DataLoadOptions..::.AssociateWith Method

Filters the objects retrieved for a particular relationship.

  NameDescription
Public methodAssociateWith<(Of <(T>)>)(Expression<(Of <(Func<(Of <(T, Object>)>)>)>)) Filters objects retrieved for a particular relationship.
Public methodAssociateWith(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.

Visual Basic
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

C#
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);
    } 
}

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker