我遇到此错误。 “实体或复杂类型'MvcApp.Models.Survey'不能在LINQ to Entities查询中构建。”C#MVC Linq subquery实体或复杂类型'XXX'不能在LINQ to Entities查询中构造
这是我的查询:
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(),但他们的视图(surveys3)失败时尝试与foreach循环模式
我的模特教室看起来像这样:
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; }
}
我也尝试过使用annonimoues类的查询,但视图失败说“传递到字典的模型产品类型的系统”。 Data.Entity.Infrastructure.DbQuery'1 [<> f__AnonymousType5'3 [System.Int64,System.String,System.String]]',但是这个字典需要一个'System.Collections.Generic.IEnumerable'类型的模型项[MvcApp.Models.Survey]'。 – Chris 2012-07-18 18:23:36
您的视图的模型是什么? – Jorge 2012-07-18 18:23:49
您好Jorge,感谢您的回答,我只是用模型更新我的问题 – Chris 2012-07-18 18:29:06