按日期和时间排列的订单列表(字符串格式)

发布于 2024-10-16 17:42:12 字数 501 浏览 2 评论 0原文

也许我要问的很简单,但我似乎没有把这个弄清楚。 我有一个包含日期字段和时间字段的元素列表。日期字段是常规日期时间,时间字段是字符串。 时间格式为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔一刀 2024-10-23 17:42:12

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

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();

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();

or via LINQ:

List = (from e in List
        orderby e.EstimatedDate, e.EstimatedTime
        select e).ToList();
抱猫软卧 2024-10-23 17:42:12

为什么不尝试如下所示的方法:

List.OrderBy(e => e.Date).ThenBy(e => DateTime.Parse(e.Time));
// May need to change DateTime.Parse(e.Time) with appropriate conversion code

why not try something like following:

List.OrderBy(e => e.Date).ThenBy(e => DateTime.Parse(e.Time));
// May need to change DateTime.Parse(e.Time) with appropriate conversion code
眼眸印温柔 2024-10-23 17:42:12

你知道 thenBy() 吗?

List = List.OrderBy(DATE).ThenBy(Time)

You know ThenBy() ?

List = List.OrderBy(DATE).ThenBy(Time)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文