我刚刚创建了 mvc 4 应用程序。在该应用程序中,我有大学表。我想根据创建日期(日期时间)列对此表数据进行排序
这是我的 Ling 查询和 Controller 方法,用于排序和列出该表
[System.Web.Mvc.Authorize]
public ActionResult U_Index(string HEI_ID = null)
{
try
{
var Institutes = from university in db.Institutes
where university.HEI_ID == HEI_ID
select university;
return View(db.Institutes.OrderByDescending(i => i.Create_Date).ToList());
}
catch (Exception ex)
{
return View();
}
}
但这不是按创建数据排序,只是想知道我错过的事情
最佳答案
在 return 语句中,您使用的是 db 变量的属性,而不是上面刚刚定义的 Institutes 变量。
return View(db.Institutes.OrderByDescending(i => i.Create_Date).ToList());
更改此设置以使用新变量:
return View(Institutes.OrderByDescending(i => i.Create_Date).ToList());
编辑:将问题改写为有效答案。详细信息提供了有效的答案,但被表述为问题。
关于c# - 根据 Date 对表进行排序,在 mvc 4 中使用 Linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30316155/