TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Anbarasan S
NA
44
8.2k
LINQ to Entities only supports casting EDM primitive or enum
Jul 19 2018 9:50 AM
Hi,
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
"
.
Below is my model class QuestionModel
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.
Reply
Answers (
1
)
select subcategory list inside category list
Replace foreach loops to LINQ