我有这个错误。“无法在 LINQ to Entities 查询中构造实体或复杂类型 'MvcApp.Models.Survey'。”
这是我的查询:
var surveys3 = (from s in db.Surveys
where s.AccountId == appAcc.Id
select new Survey
{
Id = s.Id,
Title = s.Title,
Description = s.Description,
NumberOfQuestions = (from q in s.Questions
select q).Count()
}).ToList();
View(surveys3);
我试图将 .ToList() 更改为 .AsEnumerable(),但是当尝试使用 foreach 循环模型时,它们的 View(surveys3) 失败
我的模型类如下所示:
public class Survey
{
[Required]
[Key]
public long Id { get; set; }
[Required]
[StringLength(100)]
public string Title { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[DataType(DataType.DateTime)]
public DateTime CreatedOn { get; set; }
[NotMapped]
public Int32 NumberOfQuestions { get; set; }
public virtual ICollection<Question> Questions { get; set; }
[ForeignKey("AccountId")]
public ApplicationAccount Account { get; set; }
[Required]
public long AccountId { get; set; }
}