0
votes

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; }
    }
1
Also I tried to use annonimoues class on the query, but the view fails saying "The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType53[System.Int64,System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcApp.Models.Survey]'. - Chris
What's the model of your view? - Jorge
Hello Jorge, thanks for answer, I just update my question with the model - Chris
One more thing, your view it's strongly typed to what model? - Jorge
It looks like the issue is with NumberOfQuestions. Have you tried changing things so you just get the full survey and have the NumberOfQuestion getter return Questions.Count? - SASS_Shooter

1 Answers

0
votes

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() before creating the new object. 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