Eng Mohamed Attian
Eng Mohamed Attian

Reputation: 97

Lambda expression where id in list of ids

I have 2 lists

var listquestionold = db.tblExamQuetions.Where(p => p.QuetionExamId == oldexamid).ToList();
var listquestionnew = listquestionnew = db.tblExamQuetions.Where(p => p.QuetionExamId == examid ).ToList();
List<tblExamQuestionAnswers> listanswers = new List<tblExamQuestionAnswers>();

How can I get answers where questionId is in listquestionold: listanswers =db.tblanswers.where(p=> p.ExamQuestionId exists in listquestionold ?

Upvotes: 3

Views: 12395

Answers (1)

Vsevolod Goloviznin
Vsevolod Goloviznin

Reputation: 12324

It's easy with Contains method of the List:

var listquestionold = db.tblExamQuetions.Where(p => p.QuetionExamId == oldexamid).ToList();
var listanswers = db.tblanswers.Where(w => listquestionold.Contains(w.ExamQuestionId)).ToList();

Upvotes: 10

Related Questions