Search code examples
c#.netlinqdynamics-crm-2011dynamics-crm-2016

Retrieve invoice details when retrieving invoices


I've got this simple LINQ to CRM query:

//retrieve all invoices associated to the cycle...
List<Invoice> invoiceCycleInvoices = ctx.InvoiceSet.Where(x => x.new_invoice_cycle_invoicesid.Id == invoiceCycle.Id
    && x.new_erpsync == false
    && x.StateCode != InvoiceState.Canceled).ToList();

Usually, a ToList call pulls all the relevant information that lazy loading forgets, but there is this property called invoice_details in Invoice that's always null.

How do I get it populated in one fell swoop ?


Solution

  • Access entity relationships using LoadProperty.

    foreach (var invoice in invoiceCycleInvoices)
    {
        ctx.LoadProperty(invoice, "invoice_details");
        var invoiceDetail = invoice.GetRelatedEntity<Entity>("invoice_details");
    }