我正在用C#为SharePoint网页部件编写一些自定义代码,具体来说,我正在制作一个测验,我的主要重点是处理包含问题、答案选项和正确答案的列表。
在测验的最后阶段,我需要将用户选择的答案与列表中的正确答案进行比较。目前,我正在使用以下方法来检查每个答案是否正确,但我认为这并不是很有效率,因为它会迭代每个问题。是否有一种更有效的方法,特别是针对SPList foreach循环的方法?
在测验的最后阶段,我需要将用户选择的答案与列表中的正确答案进行比较。目前,我正在使用以下方法来检查每个答案是否正确,但我认为这并不是很有效率,因为它会迭代每个问题。是否有一种更有效的方法,特别是针对SPList foreach循环的方法?
// 1. Store questions and answers in class
List<submittedAnswers> answeredQuestions = new List<submittedAnswers>();
// 2. From POST pull answered question IDs and answer IDs (which correspond to the question primary key and answer choice number both stored in the list)
// INSERT BEAUTFIUL AND EFFICIENT WHILE LOOP HERE
// 3. Loop through each question is list, if question was given, test if correct/incorrect
using (SPWeb myWeb = mySite.OpenWeb())
{
SPList answerList = myWeb.Lists[questionList];
foreach (SPListItem quizEntry in answerList.Items)
{
int pullAnswerId = int.Parse(quizEntry["Answer"].ToString()); // Pull answer number from list
int pullQuestionId = int.Parse(quizEntry["ID"].ToString()); // Pull primary key of question
submittedAnswers result = answeredQuestions.Find(delegate(submittedAnswers e) { return e.questionId == int.Parse(quizEntry["ID"].ToString()); });
if (result != null)
{
if (result.responseId != pullAnswerId) // If the response was different from the answer
incorrectAnswers++;
else
correctAnswers++;
}
}
}
// C# quiz grading magic here....