I am making my own code for a SharePoint Web Part in C #. In particular, I am doing a quiz, my main point here, referring to a list that contains a question, answers to questions and the correct answer.
At the last stage of the quiz, I need to check the answers selected by the user for the correct answer in the list. Currently, I am doing the following to verify the correctness of each of them, which I assume is not very efficient, because it iterates over each question. Is there a way, especially for the SPList foreach loop, that will be more efficient?
List<submittedAnswers> answeredQuestions = new List<submittedAnswers>();
using (SPWeb myWeb = mySite.OpenWeb())
{
SPList answerList = myWeb.Lists[questionList];
foreach (SPListItem quizEntry in answerList.Items)
{
int pullAnswerId = int.Parse(quizEntry["Answer"].ToString());
int pullQuestionId = int.Parse(quizEntry["ID"].ToString());
submittedAnswers result = answeredQuestions.Find(delegate(submittedAnswers e) { return e.questionId == int.Parse(quizEntry["ID"].ToString()); });
if (result != null)
{
if (result.responseId != pullAnswerId)
incorrectAnswers++;
else
correctAnswers++;
}
}
}
source
share