c# - Looping through a list and matching several conditions -


im trying create method grades quiz. works fine if quiz-question has 1 correct answer, i'm having hard time handleing multiple correct answers.

i first check see how many correct answers current question has. if more 1 need method checks if users answer perfect match correct answers.

so, if there question 2 correct answers, , user checked in 1 of them, not score.

any ideas on method of checking number of answers correct answers match correct answers?

i think linq fit here i'm total noob on it. maybe adding correct answers new list , matching .contains solution?

the classes like:

list<question>  question-class - list<answers>     -(string) answertext     -(bool) correctanswer  - list<string> useranswertoquestiontext 

pseudo code:

if (currentquestion has more 1 correct answer) if (useranswertext == correctanswerx && useranswertext == correctanswery)   nrofcorrectanswers++; 

should able linq

so down how model questions , answers.

make class question , make class answer this, question class contain list of answers:

public class question {     public int questionid {get; set;}     public string questiontext {get; set;}     public list<answer> answers {get; set; }  public class answer {     public int answerid {get; set;}     public int questionid {get; set;}     public string answertext {get;set;} } 

so have relationship between question , answer. check answers against answer given this:

var useranswers = new list(answer); //populate useranswers here  foreach(var question in questions) {     var answersforquestion = question.answers.select(a=>a.questionid == question.id);     if (useranswers.findall(ua => ua.questionid == questionid).length == question.answers.count())     {         //correct amount of answers. check actual answers         if (useranswers.any(ua => question.answers.contains(a=>a.id == ua.id))     //correct answers made     { } 

this assumes fixed answers questions (ie multiple choice) if have typed in answer might more tricky


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -