加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何按顺序将项目插入列表?

发布时间:2020-12-15 06:50:45 所属栏目:百科 来源:网络整理
导读:有人可以教我如何在C#中按顺序插入项目到列表中? 我有一个DateTimeOffset对象的列表,我想按顺序插入新的列表. ListDateTimeOffset TimeList = ...// determine the order before insert or add the new item 对不起,需要更新我的问题. ListcustomizedClass
有人可以教我如何在C#中按顺序插入项目到列表中?

我有一个DateTimeOffset对象的列表,我想按顺序插入新的列表.

List<DateTimeOffset> TimeList = ...
// determine the order before insert or add the new item

对不起,需要更新我的问题.

List<customizedClass> ItemList = ...
//customizedClass contains DateTimeOffset object and other strings,int,etc.

ItemList.Sort();    // this won't work until set data comparison with DateTimeOffset
ItemList.OrderBy(); // this won't work until set data comparison with DateTimeOffset

有没有人可以帮我把DateTimeOffset作为.OrderBy()的参数?

我也试过

ItemList = from s in ItemList
           orderby s.PublishDate descending    // .PublishDate is type DateTime
           select s;

但是,它返回此错误消息,

Cannot implicitly convert type ‘System.Linq.IOrderedEnumerable’ to ‘System.Collections.Gerneric.List’. An explicit conversion exist (are you missing a cast?)

解决方法

修改LINQ,最后添加ToList():
ItemList = (from s in ItemList
            orderby s.PublishDate descending   
            select s).ToList();

或者将排序的列表分配给另一个变量

var sortedList = from s in ....

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读