2012-02-14 20 views
2

如何安排学生降序?这是我的代码。安排学生降序

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()); 
     } 
} 
+1

发现降序的顺序是什么? – 2012-02-14 18:06:11

回答

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

通过

我strongley建议以此为契机,看看所有的扩展方法LINQ提供(例如排序依据,其中,选择属性,你想为了更换名称,..等),你越了解LINQ API将更方便您的生活将得到与集合

+0

+1巴桑,正好(几乎),因为我正在回答。无论如何,请留下我的 – 2012-02-14 18:09:21

+0

@jim谢谢,我知道它也发生在我身上:D – 2012-02-14 18:10:49

+0

@BassamMehanni感谢您的帮助。我确实尝试了OrderByDescening,但并不知道我需要通过这种方式传递名称。这是我第一次使用构造函数依赖注入。因此感到困惑。 – user793468 2012-02-14 18:33:50

2

user793468工作,

你不指定模式,所以只是猜测这里:

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