This is a migrated thread and some comments may be shown as answers.

Newbie Question

1 Answer 35 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mike
Top achievements
Rank 1
Mike asked on 01 Mar 2013, 11:41 AM
I am pretty new to Linq and Telerik ORM so please be gentle.
I have a database with the following tables: Customers, Invoices and InvoiceItems.
Using Linq (vb.net) I can get a list of customers doing the following:
dim results = (from c as customer in model.customers select c).ToList
I can then loop through all the customers, invoices and items as follows:
For each customer as Customer in results
  For each invoice as Invoice in customer.Invoices
    For each item as InvoiceItem in invoice.InvoiceItems
      'Do something with the invoice item
    Next
  Next
Next
The problem I am having is simple. This will result in many calls to the database. When I access customer.Invoices, the ORM tool executes a sql statement to get the invoices and so on.
I'd like to be able to write a vb.net linq query that will return all customer objects and populate the child invoices and invoice line items.

I thought the following line query would do it but it doesn't as when I loop through customer invoices a query is fired again.
dim result = (from c as customer in model.customers
     join i as invoice in model.invoices on i.customerid equals c.customerid
     join itm as invoiceitem in model.invoiceitems on itm.invoiceid equals i.invoiceid
     order by c.customerid, i.invoiceid, itm.itemid
     select c, i, itm).ToList

How would I do this?

1 Answer, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 06 Mar 2013, 11:41 AM
Hi Mike,

Telerik OpenAccess ORM allows you to define fetch plans. With them you could retrieve not only the requested objects but the related ones as well. The fetch plans utilize the FetchStrategy class which exposes two methods that achieve the immediate loading of the related objects: LoadWith and LoadWith<T>.

I suggest that you take a look at the following resources, in order to define a fetch plat that is feasible to you:
- The Define FetchPlans section in our documentation
- The Getting Started with OpenAccess Fetch Strategies video
- The Fetch Strategy API section in our documentation

If you have any questions or experience difficulties with the fetch plans, do not hesitate to get back to us.


Regards,
Doroteya
the Telerik team
OpenAccess ORM Q1 2013 is out featuring Multi-Diagrams, Persistent Data Stream Support and much more. Check out all of the latest highlights.
Tags
LINQ (LINQ specific questions)
Asked by
Mike
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
Share this question
or