按日期和时间排序(以字符串格式)

Hal*_*han 7 c# collections sql-order-by

可能我要问的很简单,但我似乎没有把这个问题拿出来.我有一个包含Date字段和Time字段的元素列表.Date字段是常规DateTime,Time字段是字符串.时间格式为HH:mm,范围为24h.

按日期排序我的列表很简单,只需按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);
Run Code Online (Sandbox Code Playgroud)

我希望有人可以帮我解决这个问题.

Mar*_*ell 14

你想要OrderBy(...).ThenBy(...);而且 - 不是如果时间在HH:mm 你不需要解析它 - 你可以按字母顺序排序,即

List = List.OrderBy(e => e.EstimatedDate).ThenBy(e => e.EstimatedTime).ToList();
Run Code Online (Sandbox Code Playgroud)

或通过LINQ:

List = (from e in List
        orderby e.EstimatedDate, e.EstimatedTime
        select e).ToList();
Run Code Online (Sandbox Code Playgroud)