How do I declare Lists in Blazor MAUI App

Anonymous
2023-11-02T16:00:29.02+00:00

How do I declare Lists in Blazor MAUI App?, the below code gives error. Blazor MAUI App giving Error with List. I want to create a quiz app using question and answers. Load questions ,answers into list. Random numbers can be increased from 1 to n.

@page "/"


<h1>Counter</h1>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>


 <p>Question</p> 
 
<div>
    <div>@question</div>
</div>
 <br>

 <p>Answer</p> 


<div>
    <div>@answer</div>
</div>
  <br>
@code {
 
    private int number;
      private Question[] questions;

    questions = new[] {
        new Question { Id = 1, Question = "Capital of France", Answer = "Paris" },
         new Question { Id = 2, Question = "Capital of Japan", Answer = "Tokyo" }
     
    };

  private void IncrementCount()
    {
        Random rand = new Random();
        int number = rand.Next(1, 3);


var question = from s in Questions
         where s.Id == number
         select s.Question;


var answer = from s in Questions
         where s.Id == number
         select s.answer;
        
}
}
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,391 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,870 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,026 Reputation points
    2023-11-02T16:55:26.7066667+00:00

    use a for loop

    for (var i = 0; i < questions.Length; ++i)
    {
      <p>Question</p> 
     
      <div>
        <div>@questions[i].Question</div>
      </div>
      <br>
    
      <p>Answer</p> 
    
      <div>
        <div>@questions[i].Answer</div>
      </div>
    }