title: Grouping foreign-key related data for grouping data by category name in View
body: <p>I'm trying to organize grouped data in my Controller to use in my View.</p>
  
  <hr>
  
  <p>I have the following solution in place but I'm receiving the following error:</p>
  
  <p><strong>"There is already an open DataReader associated with this Command which must be closed first."</strong></p>
  
  <hr>
  
  <p>Given 2 sets of related data: A set of questions and the categories they belong to, represented by the following classes</p>
  
  <pre><code>Category
  Question
  </code></pre>
  
  <p>I would like to group the questions by their category, connected by a foreign key</p>
  
  <pre><code>categoryId
  </code></pre>
  
  <p>Here's some example data:</p>
  
  <p>Categories</p>
  
  <pre><code>category1 { Id = 1, Name = "Info about you" }
  category2 { Id = 2, Name = "Info about your pet" }
  </code></pre>
  
  <p>Questions</p>
  
  <pre><code>question1 { categoryId = 1, Text = "What's your name?" }
  question2 { categoryId = 1, Text = "What's your age?" }
  question3 { categoryId = 2, Text = "What kind of pet do you have?"
  question4 { categoryId = 2, Text = "What color is your pet?"
  </code></pre>
  
  <hr>
  
  <p>And the desired HTML output would be something like</p>
  
  <p><strong>Info about you</strong></p>
  
  <ul>
  <li>What's your name?</li>
  <li>What's your age?</li>
  </ul>
  
  <p><strong>Info about your pet</strong></p>
  
  <ul>
  <li>What kind of pet do you have?</li>
  <li>What color is your pet?</li>
  </ul>
  
  <hr>
  
  <p>So my thought is to put info the category name and the associated questions into a ViewModel like the one below:</p>
  
  <pre><code>public class CategoriesWithQuestions() {
      public string Name { get; set; }
      public IEnumerable&lt;Question&gt; Questions { get; set; }
  }
  </code></pre>
  
  <p>And then in my controller I would put the data into the ViewModel using a query:</p>
  
  <pre><code>public ActionResult MyAction() {
  
      var categoriesWithQuestions =
          db.Categories()
              .Select(category =&gt; new CategoriesWithQuestions()
              {
                  Name = category.Name,
                  Questions = db.Questions().Where(Queryable =&gt; q.CategoryId == category.Id)
              });
  
      return View(categoriesWithQuestions);
  }
  </code></pre>
  
  <p>And use the ViewModel in the view, by looping it and accessing the category's name property and the associated questions, both accessors in the ViewModel class:</p>
  
  <pre><code>@model IEnumerable&lt;App.ViewModels.CategoriesWithQuestions&gt;
  
  ...
  
  @foreach (var category in Model)
  {
      &lt;h3&gt;@category.Name&lt;/h3&gt;
      &lt;ul&gt;
      @foreach (App.Models.Question Question question in category.Questions)
      {
          &lt;li&gt;@question.Text&lt;/li&gt;
      }
      &lt;/ul&gt;
  }
  </code></pre>
  
  <hr>
  
  <p>The Name comes in fine but the error happens when trying to access the Questions in the view.</p>
  
  <p>Open to ideas in fixing my current solution or alternative (better) solutions.</p>