首页 \ 问答 \ 在Date之后排序列表然后是时间(Sorting list after Date then time)

在Date之后排序列表然后是时间(Sorting list after Date then time)

所以我正在开设一个网站(MVC)来处理节日期间乐队的日程安排条目,我需要按照逻辑顺序提交预订,当我尝试订购时我遇到了一些意想不到的困难。 我的目标是先按日期和时间排序。

我显然已经阅读了所有类似的问题,但我似乎没有解决问题。

这是我的模型(在这种情况下重要的部分):

public int ScheduleEntryId { get; set; }
public int ScheduleId { get; set; }
public int? BandId { get; set; }
public int? MovieId { get; set; }
public int? TechnicianId { get; set; }
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
public DateTime EventDate { get; set; }

我试图从特定乐队获得一个排序的条目列表。 所以这就是我现在所拥有的:

List<ScheduleEntry> scheduleEntries =
    db.ScheduleEntries.Where(x => x.BandId == band.BandId)
        .OrderBy(x => x.EventDate)
        .ThenBy(z => z.StartTime.Value).ToList();

这会对日期之后的条目进行排序,但不会在时间之后排序。

我也尝试分离linq和排序,以便我可以使用.TimeOfDay,因为linq不支持该方法。 这是我试过的:

var scheduleEntries = db.ScheduleEntries.Where(x => x.BandId == band.BandId).ToList();
scheduleEntries.OrderByDescending(z => z.EventDate.Date).ThenBy(z=> z.StartTime.Value.TimeOfDay);

我知道这很傻,我可能错过了一些明显的东西,但我不能让它正常工作。 非常感谢帮助。


So I'm working on a website ( MVC ) that handles Schedule entries for bands during a festival and I need to present bookings in a logical order and I´m having some unexpected difficulties when I try to order them. My goal is to order them first by date and then by time.

I've obviously read all similar questions that I could find but none seems to solve the problem.

This is my model (The parts that matter in this situation):

public int ScheduleEntryId { get; set; }
public int ScheduleId { get; set; }
public int? BandId { get; set; }
public int? MovieId { get; set; }
public int? TechnicianId { get; set; }
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
public DateTime EventDate { get; set; }

I´m trying to get a sorted list of entries from a specific Band. So this is what I have at the moment:

List<ScheduleEntry> scheduleEntries =
    db.ScheduleEntries.Where(x => x.BandId == band.BandId)
        .OrderBy(x => x.EventDate)
        .ThenBy(z => z.StartTime.Value).ToList();

This does sort the entries after the date, but not after time.

I have also tried to seperate the linq and sorting so that I can use .TimeOfDay, since linq does not support that method. This is what I tried:

var scheduleEntries = db.ScheduleEntries.Where(x => x.BandId == band.BandId).ToList();
scheduleEntries.OrderByDescending(z => z.EventDate.Date).ThenBy(z=> z.StartTime.Value.TimeOfDay);

I know this is pretty silly and I´m probably missing something obvious, but I just cant get it to work properly. Help would be greatly appreciated.


原文:https://stackoverflow.com/questions/37568449
更新时间:2019-12-13 07:40

最满意答案

尝试

List<ScheduleEntry> scheduleEntries = db.ScheduleEntries
  .Where(x => x.BandId == band.BandId)
  .OrderBy(x => x.EventDate)
  .ThenBy(z => z.StartTime)
  .ToList();

编辑按照@ IvanStoev的评论建议


Try

List<ScheduleEntry> scheduleEntries = db.ScheduleEntries
  .Where(x => x.BandId == band.BandId)
  .OrderBy(x => x.EventDate)
  .ThenBy(z => z.StartTime)
  .ToList();

Edited As per @IvanStoev's suggestion in the comments

2016-09-03

相关问答

更多

如何减少android SQLite中的自动增量_id?(How to decrease the Auto increment _id in android SQLite?)

编辑:也许我应该说清楚只是插入具有正确id的行而不是操纵序列号肯定比下面的方法更好。 如果表中没有id = 3的行,则即使在AUTOINCREMENT表中也可以在id中插入固定值。 也就是说,如果您确定 ,可以使用自动增量值设置为任意值; UPDATE sqlite_sequence set seq=<next sequence no -1> where name=<table name>; 也就是说,如果你想在表'TableA'上的下一个插入中使用AUTOINCREMENT来生成5,那么你可

在java中插入多个记录“连接关闭后不允许任何操作”(insert multiple records in java “No operations allowed after connection closed ”)

您正在关闭最终方法中的连接(cnx)。 这将在您第一次插入时关闭连接(无论是失败还是通过),然后在您下次插入时仍将关闭连接。 如果您愿意,可以尝试将连接开口移动到此方法中,但是打开/关闭连接可能很昂贵,可能会在此方法之外移动关闭/打开。 在我们看到你正在研究的更多代码之前,我们很难说。 You are closing your connection (cnx) in the finally of your method. This will close the connection the fi

为什么我的ASP.NET复选框始终为false?(Why are my ASP.NET checkboxes always false?)

也许,当您在Page_Load上绑定数据时,您忘了这样做: if(!IsPostBack) { //bind the data to the list } Probably, when you bind the data on Page_Load you forgot to do: if(!IsPostBack) { //bind the data to the list }

Knockout js&Dynatree(Knockout js & Dynatree)

这是一个小提琴,显示Knockout通过AJAX加载视图模型,然后将渲染的元素转换为Dynatree。 http://jsfiddle.net/jearles/EZgNs/ Here is a fiddle that shows Knockout loading a View Model via AJAX and then converting the rendered elements into a Dynatree. http://jsfiddle.net/jearles/EZgNs/

排序数组的最小“插入次数”(The minimum number of “insertions” to sort an array)

首先请注意,移动元素不会更改除移动元素之外的元素的相对顺序。 考虑最长的非递减子序列(与最长的增加子序列密切相关 - 找到它们的方式相似)。 通过仅移动不在此序列中的元素,很容易看到我们最终得到一个排序列表,因为此序列中的所有元素已经相对于彼此排序。 如果我们不移动此序列中的任何元素,则保证此子序列中两个元素之间的任何其他元素要么大于较大的元素,要么小于较小的元素(如果不是这样,则它本身将在最长的序列),所以需要移动。 (见下面的例子) 它需要不减少吗? 是。 考虑此序列中的两个连续元素是否正在

脚本拼图

最新问答

更多
  • NumPy标量,广播和__iadd__方法(NumPy scalars, broadcasting and the __iadd__ method)
  • 快速排序算法输出错误(Getting wrong output for quicksort algorithm)
  • Rails管理员:没有Rails 4应用程序的编辑,显示或删除按钮(Rails Admin: No edit, show, or delete buttons for Rails 4 app)
  • 部分刷新问题触发两次(Issue with partial refresh triggered twice)
  • jsPlumb draggable element javascript函数(jsPlumb draggable element javascript function)
  • MVC4:ViewModel(带有radiobuttonlist)在HttpPost之后为空(MVC4: ViewModel (with radiobuttonlist) is empty after HttpPost)
  • 如何在同一帐户上设置“Dev repo”(在prod和团队之间)(How to set up a “Dev repo” (between the prod and the team) on the same account)
  • 如何在tcl中将eth0配置为发送方udp端口(how to configure eth0 as a sender udp port in tcl)
  • EF6.1 beta 1如何获得Tooling Consolidation功能?(EF6.1 beta 1 how to get the Tooling Consolidation feature?)
  • 适用于Windows和PHP 5.5的Phalcon(Phalcon for Windows and PHP 5.5)
  • 有没有办法忽略hg责任中的提交?(Is there a way to ignore a commit in hg blame?)
  • Desire2通过API学习访问学期的权限(Desire2Learn Permissions to Access Semesters Via API)
  • 从aspx将服务器的文件复制到另一台服务器(Copy a file of a server to another server from aspx)
  • 使用YSlow / Web Developer扩展程序确定页面大小(Figuring out page size with YSlow / Web Developer extension)
  • Select语句将字符串中的前7个字符与其他表中的其他7个字符进行比较(Select statement which compares first 7 chars in a string with other 7 chars in other table)
  • 检测对注册表的更改,防止更改等(Detect changes to registry, prevent changes, etc)
  • 用户的鼠标/键盘运动分析是否有特殊名称?(Does the analysis of mouse/keyboard motions of a user have a special name?)
  • 获取Rails中资源的路由列表(Get list of routes for a resource in Rails)
  • 用函数值除以数字(Divide a function value by a number)
  • 熊猫Python:如何从今天开始创建失效列?(Pandas Python: How to create lapse since today column?)
  • GDK Bug报告(GDK Bug Reports)
  • 困惑于在python中循环遍历列表(confused about looping through lists in python)
  • 'less'由'which'输出指定的文件('less' the file specified by the output of 'which')
  • 如何在ASP.NET中优化AJAX(How to optimize AJAX in ASP.NET)
  • 在文本文件中转换纪元时间(Convert epoch times in text file)
  • 表细胞伸展(Table cell stretching)
  • GridView控件(GridView Control)
  • 从构建定义文件控制pdb文件输出(Control pdb file output from build defintion file)
  • *将方法传递给类(*Passing a Method into a Class)
  • 如何在HttpReceiveHttpRequest()中使用HTTPS?(How to use HTTPS with HttpReceiveHttpRequest()?)