【问题标题】:Sort a Generic List with orderby or sort [duplicate]使用 orderby 对通用列表进行排序或排序 [重复]
【发布时间】:2017-03-28 16:17:09
【问题描述】:

我有一个具有这种结构的通用列表:

        public class Emoticon
    {
        public int id { get; set; }
        public string code { get; set; }
        public string emoticon_set { get; set; }
    }

    public class Emoticons
    {
        public List<Emoticon> emoticons { get; set; }
    }

列表是Emoticons twitch_emoticons。不,我想按 ID 对列表进行排序。谁能帮帮我?

【问题讨论】:

  • var sorted = twitch_emoticons.OrderBy(e =&gt; e.id);?
  • 编程不像 Matrix 中的弯曲勺子——你不仅应该想要,还应该尝试做一些事情

标签: c# list generics


【解决方案1】:

添加命名空间,

  using System.Linq;

那就试试吧,

var result = twitch_emoticons.OrderBy(res => res.id).ToList();

【讨论】:

  • 还有一点需要注意的是result 不是List&lt;T&gt;,而是IOrderedEnumerable&lt;T&gt;。这可能是也可能不是问题。如果是,您可能需要在末尾添加.ToList()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
相关资源
最近更新 更多