Share via

Dynamically increase list

Anonymous
2023-11-24T16:35:55.2033333+00:00

I am loading list from api currently I have to change the code if I increase the questions in api , the number of questions are 5 , if I increase to 10 then code has to be changed , is there any way to make this dynamic so that I don't change the code even the questions are increased

   async Task<List<InQuestion>> GetApiData()
        {
            string apiUrl = "https://sheet2api.com/v1/wlS0h0USxm9p/quiz";
            List<InQuestion> Questions = new List<InQuestion>();
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    // Make a GET request to the API
                    HttpResponseMessage response = await client.GetAsync(apiUrl);

                    // Check if the request was successful (status code 200)
                    if (response.IsSuccessStatusCode)
                    {
                        // Read the content of the response
                        string jsonString = await response.Content.ReadAsStringAsync();


                        // Deserialize the JSON string into an object
                        Questions = JsonConvert.DeserializeObject<List<InQuestion>>(jsonString);

                        // Now you can work with the 'apiData' object
                        Console.WriteLine("Received data:");

                    }
                    else
                    {
                        Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }
            }
            return Questions;
        }
        private async Task LoadQuestionsAsync()
        {

          
           

            List<InQuestion> res = await GetApiData();
           
           Question q1 = new Question
            {
                QuestionTitle = res[0].QuestionTitle,
                Options = new List<string>() { res[0].Opt1, res[0].Opt2, res[0].Opt3, res[0].Opt4 },
                Answer = res[0].Answer             };

            Question q2 = new Question
            {
                QuestionTitle = res[1].QuestionTitle,
                Options = new List<string>() { res[1].Opt1, res[1].Opt2, res[1].Opt3, res[1].Opt4 },
                Answer = res[1].Answer
            };


            Question q3 = new Question
            {
                QuestionTitle = res[2].QuestionTitle,
                Options = new List<string>() { res[2].Opt1, res[2].Opt2, res[2].Opt3, res[2].Opt4 },
                Answer = res[2].Answer
            };

            Question q4 = new Question
            {
                QuestionTitle = res[3].QuestionTitle,
                Options = new List<string>() { res[3].Opt1, res[3].Opt3, res[3].Opt3, res[3].Opt4 },
                Answer = res[3].Answer
            };



            Question q5 = new Question
            {
                QuestionTitle = res[4].QuestionTitle,
                Options = new List<string>() { res[4].Opt1, res[4].Opt3, res[4].Opt3, res[4].Opt4 },
                Answer = res[4].Answer
            };


            Questions.AddRange(new List<Question> { q1, q2, q3, q4 ,q5});
        }

  public class InQuestion 
   
        
        {

            public int No { get; set; }

            public string QuestionTitle { get; set; }

            public string Opt1 { get; set; }

            public string Opt2 { get; set; }

            public string Opt3 { get; set; }

            public string Opt4 { get; set; }

            public string Answer { get; set; }
        }
      public class Question
    {
        public string QuestionTitle { get; set; } = string.Empty;
        public IEnumerable<string> Options { get; set; } = new List<string>();
        public string Answer { get; set; } = string.Empty;
    }
Developer technologies | .NET | .NET Multi-platform App UI
0 comments No comments

Answer accepted by question author

Bruce (SqlWork.com) 84,086 Reputation points
2023-11-24T17:19:27.2966667+00:00

Just map with select:

List<InQuestion> res = await GetApiData();
Questions.AddRange(res.Select(r => new Question
{
     QuestionTitle = r.QuestionTitle,
     Options = new List<string>() {r.Opt1, r.Opt2, r.Opt3, r.Opt4},
     Answer = r.Answer            
});

         

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.