Share via


방법: 관련 데이터 필터링(LINQ to SQL)

업데이트: November 2007

AssociateWith 메서드를 사용하여 검색된 데이터의 양을 제한하기 위한 하위 쿼리를 지정합니다.

예제

다음 예제에서 AssociateWith 메서드는 Orders를 오늘 선적되지 되지 않은 것으로 제한합니다. 이 방법을 사용하지 않을 경우 한 개의 하위 집합만 필요한 경우에도 모든 Orders가 검색됩니다.

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

참고 항목

기타 리소스

데이터베이스 쿼리(LINQ to SQL)