Newbie Question

Thread is closed for posting
2 posts, 0 answers
  1. Mike
    Mike avatar
    31 posts
    Member since:
    Apr 2009

    Posted 01 Mar 2013 Link to this post

    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 ( 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
    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 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?
  2. Doroteya
    Doroteya avatar
    498 posts

    Posted 06 Mar 2013 Link to this post

    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.

    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.
Back to Top