MVC查询表达式源类型'动态'或者使用类型'动态'的连接序列不允许

时间:2018-04-20 14:31:42

标签: asp.net-core-mvc

我试图让这个表达式在我的Razor文件中运行:

<span>@($" ({string.Join(", ", from o in comment.CommentStaff.StaffOffices select o.Office.OfficeOrganizationCd)})")</span>

但它说&#34;查询表达式而不是源类型&#39;动态&#39;或者使用类型&#39;动态&#39;的连接序列不允许&#34;在Razor的红色波浪形和开发者异常页面中。

这是我的模型(@model CommentVM)从控制器动作下载到Razor:

public class CommentVM
{
    public int AuditId { get; set; }
    public string Comment { get; set; }

    public IEnumerable<Comment> Comments { get; set; }
}

这是Controller Action(实际上这是来自组件中的Invoke方法。但这不应该真的有所作为。)

public IViewComponentResult Invoke(int auditId)
    {
        IQueryable<Comment> comments = _commentRepo.Comments.Include(c => c.CommentStaff).ThenInclude(c => c.StaffOffices)
            .Where(c => c.CommentAuditId == auditId)
            .OrderByDescending(c => c.CommentDate).Take(3);

        CommentVM commentVM = new CommentVM
        {
            AuditId = auditId,
            Comments = comments
        };

        return View(commentVM);
    }

Comments Repo返回一个IQueryable of Comment对象。

评论POCO看起来像这样:

[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
}

因此,CommentStaff是Staff对象的导航属性,该对象具有名为StaffOffices的桥接表POCO,用于Staff和Office之间的多对多关系。

这是桥牌表POCO:

[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; }
}

我想要得到这个:

<span>@($" ({string.Join(", ", from o in comment.CommentStaff.StaffOffices select o.Office.OfficeOrganizationCd)})")</span>

或者这个:

@{ 
                                    StringBuilder sb = new StringBuilder();
                                    foreach(var o in comment.CommentStaff.StaffOffices)
                                    {

sb.Append(o.Office.OfficeOrganizationCd);
                                    }
                                }
                                <span>@sb.ToString()</span>

1 个答案:

答案 0 :(得分:0)

这是部分观点。我需要包含父Razor文件的模型定义:

@model CommentVM