Add Items to List Randomly

Maui Learner 480 Reputation points
2024-04-30T08:09:59.47+00:00

I am fetching 50 questions from Api https://supaquizapi.azurewebsites.net/api/Questions

Loading into list and deleting 40 questions randomly.

Now I just to add 10 questions randomly to Questions

  List<InQuestion> res = await GetApiData();
            Questions.AddRange(res.Select(r => new Question
            {
                SNo = r.No,
                Topic = r.Topic,
                QuestionTitle = r.QuestionTitle,
                Options = new List<string>() { r.Opt1, r.Opt2, r.Opt3, r.Opt4 },
                Answer = r.Answer,
                Time = r.Time,
                Correct = r.Correct,
                Solution = r.Solution
            }));
            int maxnumber = (from e in Questions select e.SNo).Max();
            int minumber = (from e in Questions select e.SNo).Min();
            int range = maxnumber + 1;
            string stopflag = "Y";
            while (stopflag == "Y")
            {
                Random rand = new Random();
                int ranumber = rand.Next(0, range);
                var result = Questions.FirstOrDefault(c => c.SNo == ranumber);
                if (result != null)
                { Questions.Remove(result); }
                int count = (from e in Questions select e.SNo).Count();
                if (count == 10)
                { stopflag = "N"; }
            }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,229 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.9K Reputation points
    2024-04-30T08:40:08.3633333+00:00

    To keep 10 questions only, try this:

    Random rand = new Random();
    
    while( Questions.Count > 10 ) 
    {
        Questions.RemoveAt(rand.Next( Questions.Count ) );
    }
    

1 additional answer

Sort by: Most helpful
  1. Maui Learner 480 Reputation points
    2024-04-30T10:18:53.54+00:00

    I added where clause

     List<InQuestion> res = await GetApiData();
                List<InQuestion> dest;
                   string stopflag = "Y";
                int maxnumber = (from e in res select e.No).Max();
                int minumber = (from e in res select e.No).Min();
                int range = maxnumber + 1;
                Random rand = new Random();
                int ranumber;
                while (stopflag == "Y")
                {
                     ranumber = rand.Next(0, range);
                    var result = Questions.FirstOrDefault(c => c.SNo == ranumber);
                //   dest= res.FirstOrDefault(c => c.No == ranumber);
                    if (result == null)
                {
                    Questions.AddRange(res.Where(r => r.No == ranumber).Select(r => new Question
                    {
                        SNo = r.No,
                        Topic = r.Topic,
                        QuestionTitle = r.QuestionTitle,
                        Options = new List<string>() { r.Opt1, r.Opt2, r.Opt3, r.Opt4 },
                        Answer = r.Answer,
                        Time = r.Time,
                        Correct = r.Correct,
                        Solution = r.Solution
                    }));
                    }
                    int count = (from e in Questions select e.SNo).Count();
                    if (count == 10)
                    { stopflag = "N"; }
                }
                /*