DataLoadOptions.LoadWith DataLoadOptions.LoadWith DataLoadOptions.LoadWith DataLoadOptions.LoadWith Method

定義

多載

LoadWith(LambdaExpression) LoadWith(LambdaExpression) LoadWith(LambdaExpression) LoadWith(LambdaExpression)

使用 lambda 運算式,擷取與主要目標相關之指定的資料。Retrieves specified data related to the main target by using a lambda expression.

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

指定當送出型別 T 物件的查詢時,要擷取的子物件。Specifies which sub-objects to retrieve when a query is submitted for an object of type T.

LoadWith(LambdaExpression) LoadWith(LambdaExpression) LoadWith(LambdaExpression) LoadWith(LambdaExpression)

使用 lambda 運算式,擷取與主要目標相關之指定的資料。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)

參數

expression
LambdaExpression LambdaExpression LambdaExpression LambdaExpression

可識別相關內容的 lambda 運算式。A lambda expression that identifies the related material.

範例

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

備註

在下列範例中,執行查詢時,會擷取位在倫敦 (London) 之所有 Orders 的所有 CustomersIn the following example, all the Orders for all the Customers who are located in London are retrieved when the query is executed. 如此一來,後續存取 Orders 物件上的 Customer 屬性時,便不會觸發新的資料庫查詢。As a result, successive access to the Orders property on a Customer object does not trigger a new database query.

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

指定當送出型別 T 物件的查詢時,要擷取的子物件。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)))

類型參數

T

要據以查詢的型別。Type that is queried against.

如果此型別不相符,就會擲回例外狀況。If this type is unmapped, an exception is thrown.

參數

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.

範例

在下列範例中,執行查詢時,會擷取位在倫敦 (London) 之所有 Orders 的所有 CustomersIn the following example, all the Orders for all the Customers who are located in London are retrieved when the query is executed. 如此一來,後續存取 Orders 物件上的 Customer 屬性時,便不會觸發新的資料庫查詢。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

備註

您不能指定兩個層級關聯性的載入 (例如Orders.OrderDetails)。You cannot specify the loading of two levels of relationships (for example, Orders.OrderDetails). 在這些案例中,您必須指定兩個不同LoadWith方法。In these scenarios you must specify two separate LoadWith methods.

若要避免循環,請參閱 < 備註 > 一節中DataLoadOptionsTo avoid cycling, see Remarks section in DataLoadOptions.

適用於