public IActionResult List() { var questions = _questionDao.GetAll(); // Convertir en view models, // en recuperant seulement les données dont la vue a besoin var QuestionListViewModels = new List <QuestionViewModel>(); if (questions != null && questions.Any()) { foreach (var question in questions) { QuestionListViewModels.Add( new QuestionViewModel() { Id = question.Id, CategoryName = question.Category.Name, TypeName = question.Type.Name, DifficultyLevel = question.Difficulty.DifficultyLevel, Content = question.Content, }); } } return(View(QuestionListViewModels)); // return des objets questions }
private IEnumerable <QuestionViewModel> GetSelectedQuestionViewModel(IEnumerable <int> selectedQuestionIds) { //récupérer la liste des questions selectionnés var questions = _questionDao.GetAll().Where(q => selectedQuestionIds.Contains(q.Id)); var questionListViewModels = new List <QuestionViewModel>(); var answers = _answerDao.GetAll(); if (questions != null && questions.Any()) { foreach (var question in questions) { questionListViewModels.Add( new QuestionViewModel() { Id = question.Id, CategoryName = question.Category.Name, TypeName = question.Type.Name, DifficultyLevel = question.Difficulty.DifficultyLevel, Content = question.Content, QuestionAnswerList = answers.Where(r => r.QuestionId == question.Id).Select(r => new CreateOrUpdateQuestionAnswerViewModel { Id = r.Id, QuestionId = r.QuestionId, QuestionContent = question.Content, AnswerContent = r.Content }).ToList() }); } } return(questionListViewModels); }
public IEnumerable <Question> GetSurvey_Questions(int surveyId) { IEnumerable <Question> questions = new List <Question>(); IEnumerable <Answer> answers = new List <Answer>(); using (SqlConnection sqlConnection1 = new SqlConnection(_connectionString)) // using permet de refermer la connection après ouverture { //ouvre la connection à la base de donnée. var cmd = new SqlCommand // objet cmd me permet d'exécuter des requêtes SQL { CommandType = CommandType.Text, // methode permettant de definir le type de commande (text = une commande sql; Storeprocedure= le nom de la procedure stockée; TableDirect= le nom d'une table. CommandText = "SELECT QuestionId FROM Survey_Question WHERE SurveyId = @surveyId;", // stock la requete sql dans commandText. SCOPE_IDENTITY renvoie l'Id de la question inseré. Connection = sqlConnection1, // etablie la connection. }; // permet de definir les variables values dans CommandText. cmd.Parameters.AddWithValue("@SurveyId", surveyId); sqlConnection1.Open(); var result = cmd.ExecuteReader(); // execute la requete et return l'element de la première ligne à la première colonne var survey_questions = new List <Survey_Question>(); while (result.Read()) { var survey_question = new Survey_Question() { SurveyId = surveyId, QuestionId = Convert.ToInt32(result["QuestionId"]) }; survey_questions.Add(survey_question); } if (survey_questions.Any()) { questions = _questionDao.GetAll()?.Where(q => survey_questions.Any(sq => sq.QuestionId == q.Id)); } //une autre manière de recupération de question. //foreach (var element in survey_questions) //{ // var getQuestionById = _questionDao.GetBydId(element.QuestionId); // var // foreach (var question in questions) // { // var q = new Question() // { // Id = getQuestionById.Id, // Content = getQuestionById.Content, // Category = getQuestionById.Category, // Difficulty = getQuestionById.Difficulty, // Type = getQuestionById.Type // }; // questions.Add(q); // } //} //}; } return(questions); }