Επεξεργασία

Κοινή χρήση μέσω


DataLoadOptions.LoadWith Method

Definition

Overloads

LoadWith(LambdaExpression)

Retrieves specified data related to the main target by using a lambda expression.

LoadWith<T>(Expression<Func<T,Object>>)

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

LoadWith(LambdaExpression)

Retrieves specified data related to the main target by using a lambda expression.

public:
 void LoadWith(System::Linq::Expressions::LambdaExpression ^ expression);
public void LoadWith (System.Linq.Expressions.LambdaExpression expression);
member this.LoadWith : System.Linq.Expressions.LambdaExpression -> unit
Public Sub LoadWith (expression As LambdaExpression)

Parameters

expression
LambdaExpression

A lambda expression that identifies the related material.

Examples

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);
}
Dim db As New Northwnd("c:\northwnd.mdf")

Dim dlo As DataLoadOptions = New DataLoadOptions()
dlo.LoadWith(Of Customer)(Function(c As Customer) c.Orders)
db.LoadOptions = dlo

Dim londonCustomers = _
    From cust In db.Customers _
    Where cust.City = "London" _
    Select cust

For Each custObj In londonCustomers
    Console.WriteLine(custObj.CustomerID)
Next

Remarks

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.

Applies to

LoadWith<T>(Expression<Func<T,Object>>)

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

public:
generic <typename T>
 void LoadWith(System::Linq::Expressions::Expression<Func<T, System::Object ^> ^> ^ expression);
public void LoadWith<T> (System.Linq.Expressions.Expression<Func<T,object>> expression);
member this.LoadWith : System.Linq.Expressions.Expression<Func<'T, obj>> -> unit
Public Sub LoadWith(Of T) (expression As Expression(Of Func(Of T, Object)))

Type Parameters

T

Type that is queried against.

If this type is unmapped, an exception is thrown.

Parameters

expression
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.

Examples

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);
}
Dim db As New Northwnd("c:\northwnd.mdf")

Dim dlo As DataLoadOptions = New DataLoadOptions()
dlo.LoadWith(Of Customer)(Function(c As Customer) c.Orders)
db.LoadOptions = dlo

Dim londonCustomers = _
    From cust In db.Customers _
    Where cust.City = "London" _
    Select cust

For Each custObj In londonCustomers
    Console.WriteLine(custObj.CustomerID)
Next

Remarks

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.

Applies to