我在C#中为SharePoint webpart做一些自定义代码。具体来说,我正在进行测验,我的主要观点是解决问题清单,答案选择和正确答案。
在测验的最后阶段,我需要检查用户根据列表中的正确答案选择的答案。目前,我正在做以下检查是否每个都是正确的,我假设它不是很有效,因为它遍历每个问题。是否有一种方法,特别是对于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....
答案 0 :(得分:3)
如果您需要访问列表中的每个项目,那么使用foreach
就完全可以了:
SPList answerList = myWeb.Lists[questionList];
foreach (SPListItem quizEntry in answerList.Items)
{
// todo...
}
通常,大多数人需要使用列表中的一部分项目。在这种情况下,您很可能希望从列表中检索项目的子集,然后进行处理。例如,通过使用SPQuery和SPList.GetItems(code from the full example here):
// 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);
仅供参考......以下是其他一些选项的良好链接:https://www.nothingbutsharepoint.com/sites/devwiki/SP2007Dev/Pages/Accessing%20list%20items%20using%20the%20object%20model.aspx
SharePoint有很多工具。始终值得确定哪种工具适合手头的工作。 :)
答案 1 :(得分:1)
这可能看起来很愚蠢,但不是使用类来存储变量,为什么不使用简单的数组呢?或者更简单的方法是编写一个sql查询来将答案数组与存储在数据库中的答案进行比较。你的解决方案似乎是很多OO魔法,但也许只是我。
另请查看答案Performance of Arrays vs. Lists
看起来使用for循环比使用foreach循环迭代列表会更快。
答案 2 :(得分:1)
我认为使用带有正确格式查询的SpList.GetItems会给出正确答案的数量。
U2U CAML Query Builder工具可用于获取正确的查询。
答案 3 :(得分:0)
为什么不在Sharepoint中使用LINQ?它非常简单,无需循环
即
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();
的指南