閱讀英文

共用方式為


DataLoadOptions.AssociateWith 方法

定義

多載

AssociateWith(LambdaExpression)

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

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

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

AssociateWith(LambdaExpression)

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

public void AssociateWith(System.Linq.Expressions.LambdaExpression expression);

參數

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

備註

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

適用於

.NET Framework 4.8.1 及其他版本
產品 版本
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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

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

public void AssociateWith<T>(System.Linq.Expressions.Expression<Func<T,object>> expression);

類型參數

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

備註

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

適用於

.NET Framework 4.8.1 及其他版本
產品 版本
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1