C# 按降序排列学生
如何按降序排列学生?这是我的密码C# 按降序排列学生,c#,asp.net-mvc,linq-to-sql,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 { //
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());
}
}
将名称替换为要按其排序的属性
我建议借此机会看看linq提供的所有扩展方法(如OrderBy、Where、Select等),了解linq API越多,使用集合就越容易
您没有指定模型,因此只需在此处猜测:
public ActionResult Index()
{
return View(_repository.ListAll().OrderByDescending(student => student.Id));
}
list.OrderByDescending(x=>x.NameofVariable).ToList()
类似的问题也可以在这里找到
+1巴萨姆,正是(几乎)在我回答的时候。离开我的anyway@jim谢谢,我知道这也经常发生在我身上:D@BassamMehanni谢谢你的帮助。我确实尝试过OrderByDescing,但不知道我需要以这种方式传递名称。这是我第一次使用构造函数依赖项注入。因此,我感到困惑。
public ActionResult Index()
{
return View(_repository.ListAll().OrderByDescending(student => student.Id));
}