Sort question by ascending order

Kalyan A 45 Reputation points
2024-07-01T17:53:22.0533333+00:00

I want to sort question by ascending order , the below statement in bold is giving error , please suggest

 public async Task<IActionResult> Details(int? id)
    {
        if (id == null || _context.Questions == null)
        {
            return NotFound();
        }
        var question = await _context.Questions.FirstOrDefaultAsync(m => m.Sno == id);
        **question = question.OrderBy(m => m.Sno);**
        if (question == null)
        {
            return NotFound();
        }
        return View(question);
    }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,344 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 60,361 Reputation points
    2024-07-01T18:18:54.8233333+00:00

    the variable question is a single entity, not a collection that can be sorted. maybe you wanted to sort before picking one:

    var question = await _context.Questions
       .OrderBy(m => m.Sno)
       .FirstOrDefaultAsync(m => m.Sno == id);
    
    0 comments No comments

0 additional answers

Sort by: Most helpful