title: LINQ Query to retrieve multiple levels of relational data
body: <p>I'm just getting up to speed with asp.net mvc and I'm wondering how one would go about getting relational data more than one level deep from the entity specified in the from clause. Using the following domain model as an example:</p>
  
  <p>A Blog has many posts. Posts have many comments.</p>
  
  <p>How would I write a LINQ query to return entities down to the Blog.Posts.Comments level?</p>
  
  <p>The only (not so elegant) solution I came up with was to use a LINQ query to get Blog and Posts and then a foreach to get comments.</p>
  
  <pre><code>var blog = (from b in _db.BlogSet.Include("Posts")
              select b);
  
  foreach (Post p in blog.Posts)
  {
      var comments = (from c in _db.CommentSet
                      where c.PostId = p.Id
                      select c);
  
      p.Comments = comments;
  
  }
  </code></pre>