为什么我不能写两个条件?我该如何解决这个问题?
why can't I write two conditions ? how can I fix this problem?
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();
我能得到两个或更多吗
来自同一列的相等性
有几种方法可以做到这一点。对 MasterQuestionSubjectId
或 Contains
使用 ||
运算符(即用 OR 代替 AND)。例如(注意额外的括号):
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();
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();
我能得到两个或更多吗 来自同一列的相等性
有几种方法可以做到这一点。对 MasterQuestionSubjectId
或 Contains
使用 ||
运算符(即用 OR 代替 AND)。例如(注意额外的括号):
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();