public int SaveTestResult(TestEntity testEntity) { using (var scope = new TransactionScope() ) { testEntity.Questions = testEntity.Questions.OrderBy(q => q.Id).ToList(); var idlist = testEntity.Questions.Select(x => x.Id).ToList(); // get ids from list of questions from testentity //note:i believe this line is causing that behaviour var questionss = _unitOfWork.QuestionRepository.GetMany(q => idlist.Contains(q.Id)).OrderBy(q => q.Id).ToList();//get questions from ids and sort them by id var questions = AutoMapper.Mapper.Map<List<Question>, List<QuestionEntity>>(questionss); //map from Question from db to QuestionEntity testEntity.Questions = testEntity.Questions.OrderBy(q => q.Id).ToList();//sort testEtities questions id to match questions above var number= 0; for (int i = 0; i < testEntity.Questions.Count; i++) { for (int j = 0; j < testEntity.Questions[i].Answers.Count; j++) { if ((testEntity.Questions[i].Answers[j].IsTrueUserChoice == questions[i].Answers[j].IsTrue) && (questions[i].Answers[j].IsTrue == true)) number++; } } testEntity.TestScore = number; testEntity.Questions = questions;//ading questions from db so model validation would pass. quesiton from testEntiy are not valid, some properties are missing var testDb = new Test(); testDb.TestTime = (testDb.TestDateEnd - testEntity.TestDateStart).Value; testDb.TestDateStart = testEntity.TestDateStart.Value; testDb.TestScore = testEntity.TestScore; testDb.UserId = testEntity.UserId; testDb.Questions = AutoMapper.Mapper.Map<List<QuestionEntity>, List<Question>>(testEntity.Questions); _unitOfWork.TestRepository.Insert(testDb); _unitOfWork.Save(); scope.Complete(); return number; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)