按日期和时间排列的订单列表(字符串格式)
也许我要问的很简单,但我似乎没有把这个弄清楚。 我有一个包含日期字段和时间字段的元素列表。日期字段是常规日期时间,时间字段是字符串。 时间格式为 HH:mm,范围为 24 小时。
通过执行 List.OrderBy(e => e.Date) 按日期排序列表很简单,但我似乎无法稍后按时间排序,以便记录的顺序根据日期和时间。
我尝试过这个,但这可能是一个很大的错误!
List = List.OrderBy(e => e.EstimatedDate).OrderBy(e => new TimeSpan(int.Parse(e.EstimatedTime.Substring(0,e.EstimatedTime.LastIndexOf(":"))),int.Parse(e.EstimatedTime.Substring(e.EstimatedTime.LastIndexOf(":")+1)),0).TotalMinutes);
我希望有人能帮助我解决这个问题。
Probably what I'm asking is quite simple but I don't seem to be getting this one out.
I have a list of elements containing a Date field and a Time field. The Date field is a regular DateTime and the Time field is a string.
Time is formatted like HH:mm and ranges in 24h.
Ordering my list by Date is simple by doing List.OrderBy(e => e.Date), but I don't seem to be able to later order it by Time so that the order of the records is according the date and the time.
I tried this out but it's probably a big mistake!
List = List.OrderBy(e => e.EstimatedDate).OrderBy(e => new TimeSpan(int.Parse(e.EstimatedTime.Substring(0,e.EstimatedTime.LastIndexOf(":"))),int.Parse(e.EstimatedTime.Substring(e.EstimatedTime.LastIndexOf(":")+1)),0).TotalMinutes);
I hope someone can help me out with this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要
OrderBy(...).ThenBy(...);
并且 - 不是如果时间是HH:mm
您不需要必须解析它 - 你可以按字母顺序排序,即或通过 LINQ:
You want
OrderBy(...).ThenBy(...);
and also - not that if the time is inHH:mm
you don't have to parse it - you can just sort it alphabetically, i.e.or via LINQ:
为什么不尝试如下所示的方法:
why not try something like following:
你知道 thenBy() 吗?
You know ThenBy() ?