I am new to the entity framework and trying to get data using foreign key from Answers table but I am getting error
The nested query is not supported. Operation1='Case' Operation2='Collect'
p.Answers.Count() or p.Answers.SingleOrDefault().correct_answeras string works fine but p.Answers.Select(c => c.correct_answer).ToList() throwing nested query error
I want to keep it in one query because there could be thousands of questions so I don't want to check answers to the separate queries. Following is my code.
return db.Questions.Where(p => p.q_id == q_id).Select(p => new QuestionViewModel
{
q_id = p.q_id,
q_text = p.q_text,
q_answer = p.Answers.Count() > 0 ? p.Answers.Select(c => c.correct_answer).ToList() : null
}).OrderBy(x => x.q_id).ToList();
ViewModel
public class QuestionViewModel
{
public long q_id { get; set; }
public string q_text { get; set; }
public List<string> q_answer { get; set; }
}