安排学生按降序排列

use*_*468 2 c# asp.net-mvc linq-to-sql

我怎样才能按降序排列学生?这是我的代码.

using System; 
using System.Collections.Generic; 
using System.Linq;
using System.Web; 
using System.Web.Mvc; 
using Student.Models;

namespace Student.Controllers {
public class StudentController : Controller

     {
         //
         // GET: /Student/

         private IStudentRepository  _repository;

         public StudentController() : this(new StudentRepository())
         {
         }

         public StudentController(IStudentRepository repository)
         {
             _repository = repository;
         }

         public ActionResult Index()
         {    
              return View(_repository.ListAll());
         } 
}
Run Code Online (Sandbox Code Playgroud)

Bas*_*nni 7

public ActionResult Index()
{    
  return View(_repository.ListAll().OrderByDescending(s => s.Name));
} 
Run Code Online (Sandbox Code Playgroud)

将Name替换为您要订购的属性

我强烈建议以此为契机来查看linq提供的所有扩展方法(例如,OrderBy,Where,Select等等),您对linq API了解得越多,您的生活就越容易使用Collections