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

适用于