我有2个清单

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>();


如何获得questionIdlistquestionold的答案:
listanswers =db.tblanswers.where(p=> p.ExamQuestionId exists in listquestionold吗?

最佳答案

使用ContainsList方法很容易:

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

10-06 12:16