c# - 根据 Date 对表进行排序,在 mvc 4 中使用 Linq 查询

标签 c# asp.net-mvc linq asp.net-mvc-4 sorting

我刚刚创建了 mvc 4 应用程序。在该应用程序中,我有大学表。我想根据创建日期(日期时间)列对此表数据进行排序

enter image description here

这是我的 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/

相关文章:

c# - Web API 和 Entity Framework ,在哪里指定数据库连接?

c# - 在 c#.net 中通过套接字序列化和反序列化多个对象

c# - 单行子查询返回多行 Oracle 数据库

c# - ASP.NET MVC 3 级联组合框不起作用

C# Linq 多或

c# - 当枚举 SortedDictionary 时,它是否按预期顺序返回 KeyValuePairs?

c# - MVC 路由问题 : null entry

c# - 使用动态列名称更新 Dapper

c# - C# 中的 System.Linq.Expressions 是干什么用的?

c# - 如何将 Linq 与 JSON 一起使用以从这个奇怪的 JSON 中获取所有 ID?