共用方式為


DataLoadOptions.AssociateWith 方法

定義

多載

AssociateWith(LambdaExpression)

篩選針對特定關聯性所擷取的物件。

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

篩選針對特定關聯性所擷取的物件。

AssociateWith(LambdaExpression)

篩選針對特定關聯性所擷取的物件。

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

參數

expression
LambdaExpression

將查詢識別為用於特定的一對多欄位或屬性。 請注意下列事項:

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

如果運算式中出現無效的運算子,則會擲回例外狀況。 下列是有效的運算子:

位置

OrderBy

ThenBy

OrderByDescending

ThenByDescending

Take

範例

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

備註

在下列範例中,內部迴圈只會逐一查看目前尚未出貨的 Orders 迴圈。

適用於

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

篩選針對特定關聯性所擷取的物件。

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

類型參數

T

要據以查詢的型別。

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

參數

expression
Expression<Func<T,Object>>

將查詢識別為用於特定的一對多欄位或屬性。 請注意下列事項:

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

如果運算式中出現無效的運算子,則會擲回例外狀況。 下列是有效的運算子:

位置

OrderBy

ThenBy

OrderByDescending

ThenByDescending

Take

範例

在下列範例中,內部迴圈只會逐一查看目前尚未出貨的 Orders 迴圈。

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

備註

如需如何避免循環的資訊,請參閱 DataLoadOptions

適用於