简体   繁体   中英

C# MVC Linq subquery The entity or complex type 'XXX' cannot be constructed in a LINQ to Entities query

I'm having this error. "The entity or complex type 'MvcApp.Models.Survey' cannot be constructed in a LINQ to Entities query."

This is my query:

        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);

I was trying to change .ToList() to .AsEnumerable(), but them the View(surveys3) fails when try to loop Model with the foreach

My model class looks like this:

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

This isn't supposed to work. Instead you could either consider creating a viewmodel to hold the fields you need (and update your view to be of that type instead of survey) and pass that to the view or call the ToList() creating the new object. 创建新对象调用ToList()。 For example:

var surveys3 =  db.Surveys.Where( s => s.AccountId == appAcc.Id)
                                .ToList()
                                .Select( s => 
                                     new Survey {
                                          Id = s.Id,
                                          Title = s.Title,
                                          Description = s.Description,
                                          NumberOfQuestions = (from q in s.Questions
                                                     select q).Count()
                                           });

EDIT: After re-reading your question and updated code, I really think what you want to do here is create a ViewModel to hold the data you need to send to the view. You don't want to create new records for what you're doing so using an entity class to hold the data the view needs is a misuse of an entity class. If you need some more info on ViewModels, read this:

http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM