Below is my code. I am assign a class member in linq where the data type is "AnswerType". During run time i got the error message as "LINQ to Entities only supports casting EDM primitive or enumeration types".
public class QuestionModel
{
public int QuestionId { get; set; }
public AnswerType AnswerType { get; set; }
public List<AnswerOption> AnswerOptions { get; set; }
}
Below is my model class AnswerType
public class AnswerType
{
public int Id { get; set; }
public string Description { get; set; }
}
Below is my Controller class method
List<QuestionModel> questions = new List<QuestionModel>();
var questionList = PoletusHrSuiteContext.Questions
.Where(question => question.ClientInterviewingPositionId == interviewInformaton.InterviewingPosition.InterviewingPositionId)
.Select (question => new Models.QuestionModel {
QuestionId = question.QuestionId,
AnswerType = (Models.AnswerType) (from mapping in PoletusHrSuiteContext.QuestionAnswerTypeMappings
join answerType in PoletusHrSuiteContext.AnswerTypes on mapping.AnswerTypeId equals answerType.AnswerTypeId
where question.QuestionId == mapping.QuestionId
select new Models.AnswerType { Id = answerType.AnswerTypeId, Description = answerType.AnswerTypeDescription })
});
questions = questionList.ToList();
Below is error message i am during run-time.
Unable to cast the type 'System.Linq.IQueryable`1[[Models.AnswerType, Models, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' to type 'Models.AnswerType'. LINQ to Entities only supports casting EDM primitive or enumeration types.
How to overcome this issue?
Thanks in advance for the solving this issue.