영어로 읽기

다음을 통해 공유


방법: 한 번에 여러 개체 검색

LoadWith를 사용하여 한 쿼리에서 여러 개체를 검색할 수 있습니다.

예시

다음 코드에서는 LoadWith 메서드를 사용하여 CustomerOrder 개체를 모두 검색합니다.

Northwnd db = new Northwnd(@"northwnd.mdf");
DataLoadOptions ds = new DataLoadOptions();
ds.LoadWith<Customer>(c => c.Orders);
ds.LoadWith<Order>(o => o.OrderDetails);
db.LoadOptions = ds;

var custQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

foreach (Customer custObj in custQuery)
{
    Console.WriteLine($"Customer ID: {custObj.CustomerID}");
    foreach (Order ord in custObj.Orders)
    {
        Console.WriteLine($"\tOrder ID: {ord.OrderID}");
        foreach (OrderDetail detail in ord.OrderDetails)
        {
            Console.WriteLine($"\t\tProduct ID: {detail.ProductID}");
        }
    }
}

참고 항목