title: MVC Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
body: <p>I am trying to get this expression working in my Razor file:</p>
  
  <pre><code>&lt;span&gt;@($" ({string.Join(", ", from o in comment.CommentStaff.StaffOffices select o.Office.OfficeOrganizationCd)})")&lt;/span&gt;
  </code></pre>
  
  <p>But it says "Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed" in the red squiggly in the Razor and the Developer exception page.</p>
  
  <p>Here is my model (@model CommentVM) coming down to the Razor from the controller action:</p>
  
  <pre><code>public class CommentVM
  {
      public int AuditId { get; set; }
      public string Comment { get; set; }
  
      public IEnumerable&lt;Comment&gt; Comments { get; set; }
  }
  </code></pre>
  
  <p>Here is the Controller Action (actually this is coming from the Invoke method in a component. But this shouldn't really make a difference).</p>
  
  <pre><code>public IViewComponentResult Invoke(int auditId)
      {
          IQueryable&lt;Comment&gt; comments = _commentRepo.Comments.Include(c =&gt; c.CommentStaff).ThenInclude(c =&gt; c.StaffOffices)
              .Where(c =&gt; c.CommentAuditId == auditId)
              .OrderByDescending(c =&gt; c.CommentDate).Take(3);
  
          CommentVM commentVM = new CommentVM
          {
              AuditId = auditId,
              Comments = comments
          };
  
          return View(commentVM);
      }
  </code></pre>
  
  <p>The Comments Repo returns an IQueryable of Comment objects.</p>
  
  <p>The Comment POCO looks like this:</p>
  
  <pre><code>[Table("comment")]
  public class Comment
  {
      [Key]
      [Column("comment_id")]
      public int CommentId { get; set; }
  
      [Column("comment_type_cd")]
      public string CommentTypeCd { get; set; }
  
      [Column("comment_audit_id")]
      public int? CommentAuditId { get; set; }
  
      [Column("comment_finding_id")]
      public int? CommentFindingId { get; set; }
  
      [Column("comment_recommend_id")]
      public int? CommentRecommendId { get; set; }
  
      [Column("comment_action_item_id")]
      public int? CommentActionItemId { get; set; }
  
      [Column("comment_acd_id")]
      public int? CommentAcdId { get; set; }
  
      [Column("comment_pdl_id")]
      public int? CommentPdlId { get; set; }
  
      [Column("comment_cost_nm")]
      public string CommentCostNm { get; set; }
  
      [Required]
      [Column("comment_tx")]
      public string CommentText { get; set; }
  
      [Column("comment_dt")]
      public DateTime CommentDate { get; set; }
  
      #region Navigation Properties
      [Column("comment_staff_id")]
      public short CommentStaffId { get; set; }
      [ForeignKey("CommentStaffId")]
      public Staff CommentStaff { get; set; }
      #endregion
  }
  </code></pre>
  
  <p>So CommentStaff is a navigation property to a Staff object which has a bridge table POCO called StaffOffices for a many to many relationship between Staff and Office.</p>
  
  <p>Here is the bridge table POCO:</p>
  
  <pre><code>[Table("staff_office")]
  public class StaffOffice
  {
      [Column("staff_office_staff_id")]
      public short ID { get; set; }
      public Staff Staff { get; set; }
  
      [Column("staff_office_office_id")]
      public short OfficeID { get; set; }
      public Office Office { get; set; }
  }
  </code></pre>
  
  <p>I am trying to get this:</p>
  
  <pre><code>&lt;span&gt;@($" ({string.Join(", ", from o in comment.CommentStaff.StaffOffices select o.Office.OfficeOrganizationCd)})")&lt;/span&gt;
  </code></pre>
  
  <p>or this:</p>
  
  <pre><code>@{ 
                                      StringBuilder sb = new StringBuilder();
                                      foreach(var o in comment.CommentStaff.StaffOffices)
                                      {
  
  sb.Append(o.Office.OfficeOrganizationCd);
                                      }
                                  }
                                  &lt;span&gt;@sb.ToString()&lt;/span&gt;
  </code></pre>