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

Posted on 2009-12-29
Last Modified: 2013-11-11
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?
Question by:JElster
    LVL 12

    Expert Comment

    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;

    LVL 1

    Author Comment

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

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

      LoadOperation<Customers> lo = DS.Load(query);
    LVL 12

    Accepted Solution


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now