title: return LINQ query to view and loop through the results in the view
body: <p>I am running in to a problem where I have made the following query in my controller:</p>
  
  <pre><code>var query = from pmt in db.ProjectHasTags
              join project in db.Projects on pmt.ProjectId equals project.ID
              join tag in db.ProjectTags
                   on pmt.TagId equals tag.ID
                   group pmt by pmt.Project into pmtGroup
                      select new
                      {
                          Project = pmtGroup.Key,
                          Tags = pmtGroup.Select(project =&gt; project.ProjectTag)
                      };
  </code></pre>
  
  <p>I want to return this query to a view using:</p>
  
  <pre><code>return View(query.ToList());
  </code></pre>
  
  <p>In the view file I have the following code:</p>
  
  <pre><code>@model IEnumerable&lt;portfolio.Models.ProjectHasTag&gt;
  
  @foreach (var p in Model)
  {
      @p.Project.Title
  
      foreach (var tag in p.Tags)
      {
          @tag.title
      }       
  }
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>The model item passed into the dictionary is of type 'System.Collections.Generic.List<code>1[&lt;&gt;f__AnonymousType6</code>2[portfolio.Models.Project,System.Collections.Generic.IEnumerable<code>1[portfolio.Models.ProjectTag]]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable</code>1[portfolio.Models.ProjectHasTag]'.</p>
  </blockquote>
  
  <p>The ProjectHasTag model code:</p>
  
  <pre><code>public class ProjectHasTag
  {
      public int ID { get; set; }
  
      public int? ProjectId { get; set; }
  
      [ForeignKey("ProjectId")]
      [DisplayName("Project")]
      public virtual Project Project { get; set; }
  
      public int? TagId { get; set; }
  
      [ForeignKey("TagId")]
      [DisplayName("Tag")]
      public virtual ProjectTag ProjectTag { get; set; }
  
      public virtual ICollection&lt;ProjectTag&gt; Tags { get; set; }
  
  }
  </code></pre>
  
  <p>This is what I want to achieve:
  <a href="http://i.stack.imgur.com/DAZ5n.png" rel="nofollow">http://i.stack.imgur.com/DAZ5n.png</a> (I cant post images yet)</p>
  
  <p>thanks for taking the time, English is not my first language.</p>