ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,229 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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"; }
}
To keep 10 questions only, try this:
Random rand = new Random();
while( Questions.Count > 10 )
{
Questions.RemoveAt(rand.Next( Questions.Count ) );
}
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"; }
}
/*