The most efficient C # SharePoint list iteration

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?

                // 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....
+5
source share
4 answers

, foreach :

SPList answerList = myWeb.Lists[questionList];
foreach (SPListItem quizEntry in answerList.Items)
{
    // todo...
}

, . , , . , SPQuery SPList.GetItems( ):

// Build a query.
SPQuery query = new SPQuery();
query.Query = string.Concat(
                    "<Where><Eq>",
                        "<FieldRef Name='Status'/>",
                        "<Value Type='CHOICE'>Not Started</Value>",
                    "</Eq></Where>",
                    "<OrderBy>",
                        "<FieldRef Name='DueDate' Ascending='TRUE' />",
                        "<FieldRef Name=’Priority’ Ascending='TRUE' />", 
                    "</OrderBy>");                    

query.ViewFields = string.Concat(
                          "<FieldRef Name='AssignedTo' />",
                          "<FieldRef Name='LinkTitle' />",
                          "<FieldRef Name='DueDate' />",
                          "<FieldRef Name='Priority' />");

query.ViewFieldsOnly = true; // Fetch only the data that we need.

// Get data from a list.
string listUrl = web.ServerRelativeUrl + "/lists/tasks";
SPList list = web.GetList(listUrl);
SPListItemCollection items = list.GetItems(query);

FYI... , : https://www.nothingbutsharepoint.com/sites/devwiki/SP2007Dev/Pages/Accessing%20list%20items%20using%20the%20object%20model.aspx

SharePoint . , .:)

+3

, , ? sql- , . , , , , , .

, for, foreach, .

+1

LINQ Sharepoint?

SPLinqDataContext dc = new SPLinqDataContext(SPContext.Current.Web.Url);

EntityList<QuizItem> Answers = dc.GetList<QuizItem>("Quiz");
EntityList<QuestionsItem> Questions = dc.GetList<QuestionsItem>("Questions");

int iCorrectAnswers = (from q in Questions
            from a in Answers
            where (q.Question == a.Question) && (q.CorrectAnswer == a.Answer)
            select a).Count();

int iWrongAnswers = (from q in Questions
                        from a in Answers
                        where (q.Question == a.Question) && (q.CorrectAnswer != a.Answer)
                        select a).Count();

LINQ Sharepoint

0

All Articles