3

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 ?

Francis Ducharme
  • 4,848
  • 6
  • 43
  • 81

2 Answers2

1

Access entity relationships using LoadProperty.

foreach (var invoice in invoiceCycleInvoices)
{
    ctx.LoadProperty(invoice, "invoice_details");
    var invoiceDetail = invoice.GetRelatedEntity<Entity>("invoice_details");
}
dynamicallyCRM
  • 2,980
  • 11
  • 16
0

It should be:

List<Invoice> invoiceCycleInvoices = ctx.InvoiceSet
    .Where(x => x.new_invoice_cycle_invoicesid.Id == invoiceCycle.Id
        && x.new_erpsync == false
        && x.StateCode != InvoiceState.Canceled)
    .Include(x => x.invoice_details)
    .ToList();
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232