根据日期对列表进行排序时,OrderBy不会更改列表

编程入门 行业动态 更新时间:2024-10-11 15:14:41
本文介绍了根据日期对列表进行排序时,OrderBy不会更改列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个结构体和一个如下列表,我只是想根据日期对输入点进行排序.我使用了以下命令,但看不到任何排序.

I have a struct and a list as follow, I just wanted to sort the Inputpoints according to date. I have used the following commands but I can not see any sorting.

public struct Points { public Date Date; public double Quantity; } _test = new List<Points>(InputPoints); _test.OrderBy(t => t.Date);

推荐答案

调用_test.OrderBy(t => t.Date)不会不会更改_test本身的内容,而是返回已排序的IOrderedEnumerable<Points>.您可以使用ToList()将其转换为List<Points>.总而言之

Calling _test.OrderBy(t => t.Date) does not change the contents of _test itself, but rather returns a sorted IOrderedEnumerable<Points>. You can turn this back into a List<Points> using ToList(). So all in all

_test = _test.OrderBy(t => t.Date).ToList();

应该做你想做的事.

更多推荐

根据日期对列表进行排序时,OrderBy不会更改列表

本文发布于:2023-11-13 11:06:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584194.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   日期   OrderBy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!