按日期和时间排序的列表(字符串格式) [英] Order List by Date and Time (in string format)
问题描述
也许我问的是很简单,但我似乎没有得到这一个。
我有一个包含Date字段和Time字段的元素列表。日期字段是常规的DateTime,时间字段是字符串。
时间格式为HH:mm,范围为24小时。
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.
按日期排序我的列表很简单,List.OrderBy(e => e.Date ),但我似乎不能以后按时间排序,所以记录的顺序是根据日期和时间。
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.
推荐答案
你想要 OrderBy(...)。ThenBy(...);
时间在 HH:mm
您不必解析 - 您只需按字母顺序排序,即
You want OrderBy(...).ThenBy(...);
and also - not that if the time is in HH:mm
you don't have to parse it - you can just sort it alphabetically, i.e.
List = List.OrderBy(e => e.EstimatedDate).ThenBy(e => e.EstimatedTime).ToList();
或通过LINQ:
List = (from e in List
orderby e.EstimatedDate, e.EstimatedTime
select e).ToList();
这篇关于按日期和时间排序的列表(字符串格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!