private void AddQuestionToTakenQuestions(QuestionClosedAnswerTestVm question, bool isCorrect, User user, DateTime date) { List <ClosedAnswer> answers = new List <ClosedAnswer>(); var questionEntity = (QuestionClosedAnswer)this.Context.Questions.Find(question.Id); for (int i = 0; i < questionEntity.Answers.Count; i++) { answers.Add(new ClosedAnswer() { Id = question.AnswerVms[i].Id, IsTrue = questionEntity.Answers[i].IsTrue, Question = question.AnswerVms[i].Question, Text = question.AnswerVms[i].Text }); } int?choosenAnswerId = null; if (question.AnswerVms.Any(a => a.IsChecked)) { choosenAnswerId = question.AnswerVms.FirstOrDefault(a => a.IsChecked).Id; } TakenQuestion takenQuestion = new TakenQuestion() { Question = this.Context.Questions.FirstOrDefault(q => q.Id == question.Id), DateOfTake = date, IsCorrect = isCorrect, ChoosenAnswer = this.Context.ClosedAnswers.FirstOrDefault(a => a.Id == choosenAnswerId), }; this.Context.TakenQuestions.Add(takenQuestion); this.Context.SaveChanges(); }
private bool IsCorrectQuestionClsAns(QuestionClosedAnswerTestVm question) { var questionEntity = (QuestionClosedAnswer)this.Context.Questions.Find(question.Id); bool flag = false; for (int i = 0; i < questionEntity.Answers.Count; i++) { if (question.AnswerVms[i].IsChecked && questionEntity.Answers[i].IsTrue) { flag = true; } } return(flag); }