DataLoadOptions.LoadWith 方法

定義

多載

LoadWith(LambdaExpression)

使用 lambda 運算式,擷取與主要目標相關之指定的資料。

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

指定當送出型別 T 物件的查詢時,要擷取的子物件。

LoadWith(LambdaExpression)

使用 lambda 運算式,擷取與主要目標相關之指定的資料。

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

可識別相關內容的 lambda 運算式。

範例

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 的所有 Customers。 如此一來,後續存取 Orders 物件上的 Customer 屬性時,便不會觸發新的資料庫查詢。

適用於

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

指定當送出型別 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

要據以查詢的型別。

如果此型別不相符,就會擲回例外狀況。

參數

expression
Expression<Func<T,Object>>

識別要擷取的欄位或屬性。

如果運算式不是識別為表示一對一或一對多關聯性的欄位或屬性,則會擲回例外狀況。

範例

在下列範例中,執行查詢時,會擷取位在倫敦 (London) 之所有 Orders 的所有 Customers。 如此一來,後續存取 Orders 物件上的 Customer 屬性時,便不會觸發新的資料庫查詢。

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) 。 在這些案例中,您必須指定兩個不同的 LoadWith 方法。

若要避免迴圈,請參閱 中的一 DataLoadOptions節。

適用於