List<MasterQuestion> questions = _sql.MasterQuestions
.Include(x => x.MasterQuestionVariants)
.Where(x => x.MasterQuestionExamId == ed.ExamMasterDetailExamId
&& x.MasterQuestionSectorId == ed.ExamMasterDetailSectorId
&& x.MasterQuestionSubjectId == 6
&& x.MasterQuestionSubjectId == 7)
.ToList();
我可以從同一列中獲得兩個或更多相等嗎
uj5u.com熱心網友回復:
有幾種方法可以做到這一點。對or使用||運算子(即 OR 而不是 AND)。例如(注意額外的括號):MasterQuestionSubjectIdContains
List<MasterQuestion> questions = _sql.MasterQuestions
.Include(x => x.MasterQuestionVariants)
.Where(x => x.MasterQuestionExamId == ed.ExamMasterDetailExamId
&& x.MasterQuestionSectorId == ed.ExamMasterDetailSectorId
&& (x.MasterQuestionSubjectId == 6 || x.MasterQuestionSubjectId == 7))
.ToList();
或者這更具可讀性和可擴展性,因為您可以向陣列添加多個 ID,并且查詢不會變得更糟:
var ids = new [] { 6, 7 };
List<MasterQuestion> questions = _sql.MasterQuestions
.Include(x => x.MasterQuestionVariants)
.Where(x => x.MasterQuestionExamId == ed.ExamMasterDetailExamId
&& x.MasterQuestionSectorId == ed.ExamMasterDetailSectorId
&& ids.Contains(x.MasterQuestionSubjectId))
.ToList();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/453230.html
