安排学生按降序排列

时间:2012-02-14 18:01:39

标签: 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());
         } 
}

3 个答案:

答案 0 :(得分:7)

public ActionResult Index()
{    
  return View(_repository.ListAll().OrderByDescending(s => s.Name));
} 

将名称替换为您想要按

排序的属性

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

答案 1 :(得分:2)

user793468,

你没有指定模型,所以在这里猜测:

public ActionResult Index()
{    
  return View(_repository.ListAll().OrderByDescending(student => student.Id));
} 

答案 2 :(得分:0)

list.OrderByDescending(x => x.NameofVariable)。ToList();

这里可以找到类似的问题 C# list.Orderby descending

相关问题