Link to home
Start Free TrialLog in
Avatar of JElster
JElsterFlag for United States of America

asked on

Visual Studio - LINQ to SQL Class - 1 to Many - How to return children

Hi..
I have LINQ to SQL class I created in the Visual Studio LINQ to SQL designer. It's simple 1 to many. Like customers to orders.
I created a 1 to many Association in the designer.  My class that is generated for Customers contains an Entity property for Orders.
When I run my GetCustomersQuery() - the data is returned but the Entity property with the Orders is empty.  How do return the Customers with the corresponding Orders?
thanks
Avatar of kumar754
kumar754
Flag of United States of America image

DataLoadOptions loadOptions = new DataLoadOptions();
activeLoad.LoadWith<Customers>(c => c.Orders);

context.LoadOptions = loadOptions;

var customers = from customer in context.customers
                        select customer;

// this will all Orders (if exists for a customer) and in your code you can access ordres ust by using

Customer[] allCustomers = GetCustomerQuery();
foreach (Customer c in allCustomers)
{
    if (c.Order != null) int orderId = c.Order.OrderID;
}

Avatar of JElster

ASKER

Hi.. My query looks like this.. how do I include the Orders.. thanks again

    var query = from c in DS.GetCustomersQuery()
                        orderby c.name
                        select c;

  LoadOperation<Customers> lo = DS.Load(query);
ASKER CERTIFIED SOLUTION
Avatar of kumar754
kumar754
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial