title: In EF 4.1 eager loading, how can I load multiple (same-level) grandchild relationships?
body: <p>I'm seemingly stumped by a likely simple solution. I've typically used lazy loading in EF 4.1, and I'm now trying to use eager loading in my application so I can use the built-in JSON serializer without issues. The problem I am having is that I can't figure out how to use .Include() to load multiple same-level grandchild relationships.</p>
  
  <pre><code>public class Canvas
  {
      public int ID { get; set; }
      public string Name { get; set; }
  
      public ICollection&lt;ContentArea&gt; Contents { get; set; }
  }
  
  public class ContentArea
  {
      public int ID { get; set; }
      public int CanvasID { get; set; }
      public string Name { get; set; }
  
      public ICollection&lt;TextContent&gt; TextContents { get; set; }
      public ICollection&lt;ImageContent&gt; ImageContents { get; set; }
  }
  
  public class TextContent
  {
      public int ID { get; set; }
      public int ContentAreaID { get; set; }
      public string Text { get; set; }
      public int Color { get; set; }
  }
  
  public class ImageContent
  {
      public int ID { get; set; }
      public int ContentAreaID { get; set; }
      public string Filename { get; set; }
  }
  </code></pre>
  
  <p>I've tried the following with no success. How can I write the loading code to load TextContents and ImageContents?</p>
  
  <p>Doesn't compile:</p>
  
  <pre><code>var c = dataContext.Canvases
          .Include(ca =&gt; ca.Contents.Select(co =&gt; co.ImageContents).Select(co =&gt; co.TextContents))
          .FirstOrDefault();
  </code></pre>
  
  <p>Doesn't work, second Include overrides first:</p>
  
  <pre><code>var c = dataContext.Canvases
          .Include(ca =&gt; ca.Contents.Select(co =&gt; co.ImageContents))
          .Include(ca =&gt; ca.Contents.Select(co =&gt; co.TextContents))
          .FirstOrDefault();
  </code></pre>
  
  <p>Doesn't work, throws runtime exception:</p>
  
  <pre><code>var c = dataContext.Canvases
          .Include(ca =&gt; ca.Contents.Select(co =&gt; new { co.ImageContents, co.TextContents }))
          .FirstOrDefault();
  </code></pre>
  
  <p>Edit:</p>
  
  <p>I've given up on this approach for now and just made view models based on some other articles and approaches they have taken solving the "serializing Entity models" problem with the ASP.NET MVC built in JSON serialization. This caused me to duplicate my classes, but it was made easy by using the AutoMapper library to transfer all of the data back and forth automatically.</p>