title: Cast EF DBquery to Viewmodel - Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery
body: <p>I have a Entity Framework query who group the result, I want to pass it to view, but I cant cast it correct to the viewmodel! (I want to show the result in a grid in the view)</p>
  
  <p>If I use this cast</p>
  
  <pre><code> viewModel.Groupeds = (IEnumerable&lt;Grouped&gt;) result;
  </code></pre>
  
  <p>I get these error
  Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery<code>1[&lt;&gt;f__AnonymousType4</code>2[System.Nullable`1[System.Int32],System.Int32]]' to type </p>
  
  <p>How should I cast it?</p>
  
  <pre><code>public ActionResult Show(int? id)
      {
         IEnumerable&lt;dynamic&gt; result = db.StatData
               .GroupBy(k =&gt; new { k.QuestId, k.OptionId })
               .Select(c =&gt; new
               {
                   OptionId = c.Select(q =&gt; q.OptionId).FirstOrDefault(),
                   Counted = c.Select(q =&gt; q.id).Count(),
               });
  
               var viewModel = new StatsView();
               viewModel.Groupeds = (IEnumerable&lt;Grouped&gt;) result;
      return View(viewModel);
  }
  
  public class StatsView
  {
      public IEnumerable&lt;Grouped&gt; Groupeds { get; set; }
  }
  
  
  public partial class Grouped
  {
      public Nullable&lt;int&gt; Counted { get; set; }
      public Nullable&lt;int&gt; OptionId { get; set; }
  }
  </code></pre>