title: Entity Framework Explicit Load in Batch
body: <p>I need to load up a bunch of data for generating a report. About 8 or 9 different entity types in a graph are involved.</p>
  
  <p>If I call <code>Include</code> to include all the data I need, the resulting query is so complex (left joins, unions, case statements galore) that it takes about 600 seconds to execute.</p>
  
  <p>If I let lazy loading lazy load the data for me as I generate the report, it takes about 180 seconds to create the report. Better, but still not acceptable.</p>
  
  <p>If I do a </p>
  
  <blockquote>
    <p>"load the next entity type based on the ids of the related entity
    type"</p>
  </blockquote>
  
  <p>approach (sort of like LLBLGen if you're familiar with it), I can get all the data I need in about 3 seconds.</p>
  
  <p>Any suggestions on how to do this?</p>
  
  <p>Essentially I want to do something like this:</p>
  
  <pre><code>var invoices = objectContext.Invoices.Where(...reportCriteria...).ToArray();
  
  objectContext.LoadProperty(invoices, "InvoiceReceivables");
  
  objectContext.LoadProperty(invoices.SelectMany(i =&gt; i.InvoiceReceivables), "Adjustments");
  
  objectContext.LoadProperty(invoices.SelectMany(i =&gt; i.InvoiceReceivables.SelectMany(ir =&gt; ir.Adjustments)), "AdjustmentComments")
  
  ... and so on
  </code></pre>
  
  <p>But the LoadProperty only works for single entities, not collections.</p>
  
  <p>Is there any way other than doing the queries and building up the object graph myself?</p>