Tester
Tester

Reputation: 2967

C# index list of objects

How do I use an index in C# in order to keep track of a list of questions?

The list has five questions, so that when the first one is loaded on the quiz page, the user provides an answer with the click of a button, which then goes to the answer page, which tells the user if they were right or wrong. On the answer page the user will click a "next question" button, which should load the next question in the list back on the quiz page. This should some how keep the index , so that the code knows that the second question is now to be loaded.

This gets the full list of questions:

public static IEnumerable<QuizGroups> GetGroups(string sectorId)
{                
    var Quizes = _QuizDataSource.AllQuizGroups.Where(x => x.Subtitle == sectorId);
    return Quizes;    
}

How do I use an index to iterate through the list of questions? There are 5 questions in the list.

Upvotes: 0

Views: 143

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460138

There are 5 questions in the list.

You don't return a List<QuizGroups> but an IEnumerable<QuizGroups>. That's probably an "unmaterialized" query and not a collection. Otherwise you could use it's indexer directly to acces an item.

If there are only 5 items returned you could use ToList to create a real list. If you only need 5 and you want to ensure that, you can use Take(5) before:

public static IList<QuizGroups> GetGroups(string sectorId) 
{
    var Quizes = _QuizDataSource.AllQuizGroups
        .Where(x => x.Subtitle == sectorId)
        .OrderBy(x => CreatedAt)
        .Take(5)
        .ToList();
    return Quizes;
}

Now you can use the indexer or Enumerable.ElementAt:

IList<QuizGroups> groups = GetGroups(sectorID);
QuizGroups quiz1 = groups[0]; // via indexer
QuizGroups quiz2 = groups.ElementAt(1); // via ElementAt, can throw an exception if there are less than 2
QuizGroups quiz3 = groups.ElementAt(2); // via ElementAtOrDefault, null if there are less than 3
// ...

Note that you don't need to use ToList in GetGroups to create a collection first. You could use ElementAt directly, but then you would always execute the query when you access the next index.

Edit according to your new background informations: I assume that you need to order the list by a DateTime property to ensure the correct order. Therefore i've added OrderBy in the query in GetGroups.

Upvotes: 2

Florian
Florian

Reputation: 1887

Get a list from the Quizes with

var quizesList = Quizez.ToList(); 

and then:

for(int index = 0; index < quizesList.Count(); index++)
{
     //Access Item at Index with quizesList[index];
}

or you can use ElementAt(int index)

  for(int index = 0; index < Quizes.Count(); index++)
    {
         //Access Item at Index with Quizes.ElementAt(index);
    }

Upvotes: 0

TGH
TGH

Reputation: 39248

Do you need the index for anything else in your code. If not, I would use a foreach

foreach(var quizGroup in GetGroups(1))
{
   //you now have a reference to each quiz group as you're going through the list
}

Or for individual access

var q = GetGroups(1).ToList();
q[0]
q[1]
...
q[q.Count - 1]

Upvotes: 0

Related Questions