Export (0) Print
Expand All

DataLoadOptions.LoadWith<T> Method (Expression<Func<T, Object>>)

Specifies which sub-objects to retrieve when a query is submitted for an object of type T.

Namespace:  System.Data.Linq
Assembly:  System.Data.Linq (in System.Data.Linq.dll)

public void LoadWith<T>(
	Expression<Func<T, Object>> expression
)

Type Parameters

T

Type that is queried against.

If this type is unmapped, an exception is thrown.

Parameters

expression
Type: System.Linq.Expressions.Expression<Func<T, Object>>

Identifies the field or property to be retrieved.

If the expression does not identify a field or property that represents a one-to-one or one-to-many relationship, an exception is thrown.

You cannot specify the loading of two levels of relationships (for example, Orders.OrderDetails). In these scenarios you must specify two separate LoadWith methods.

To avoid cycling, see Remarks section in DataLoadOptions.

In the following example, all the Orders for all the Customers who are located in London are retrieved when the query is executed. As a result, successive access to the Orders property on a Customer object does not trigger a new database query.

Northwnd db = new Northwnd(@"c:\northwnd.mdf");
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Customer>(c => c.Orders);
db.LoadOptions = dlo;

var londonCustomers =
    from cust in db.Customers
    where cust.City == "London" 
    select cust;

foreach (var custObj in londonCustomers)
{
    Console.WriteLine(custObj.CustomerID);
}

.NET Framework

Supported in: 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft