如何通过嵌套的IEnumerable对象对IEnumerable对象进行排序?

时间:2012-09-03 02:45:49

标签: c# linq

  

可能重复:
  Sort a field of each object in a list with LINQ

我有这个系列:

    IEnumerable<sample> sam = new List<sample>()
    {
        new sample{ id = 1, name = "sample 1", list = new List<int>{5,6,1}},
        new sample{ id = 2, name = "sample 1", list = new List<int>{2,9}},
        new sample{ id = 3, name = "sample 1", list = new List<int>{8,3,7}},
        new sample{ id = 4, name = "sample 1", list = new List<int>{8,4,3}},
        new sample{ id = 5, name = "sample 1", list = new List<int>{5,1,7}},
        new sample{ id = 6, name = "sample 1", list = new List<int>{6,9,7}}
    };

如何使用LINQ对其进行排序,以便输出为:

     new sample{ id = 2, name = "sample 1", list = new List<int>{2,9}},
     new sample{ id = 5, name = "sample 1", list = new List<int>{5,1,7}},
     new sample{ id = 1, name = "sample 1", list = new List<int>{5,6,1}},
     new sample{ id = 6, name = "sample 1", list = new List<int>{6,9,7}}
     new sample{ id = 3, name = "sample 1", list = new List<int>{8,3,7}},
     new sample{ id = 4, name = "sample 1", list = new List<int>{8,4,3}},

顺便说一句,这就是'列表'。

由于

1 个答案:

答案 0 :(得分:2)

以下是sam属性的第一项命令list的LINQ(我收集的是你所追求的,对吧?):

IEnumerable<sample> orderedSam = sam.OrderBy(item => item.list.FirstOrDefault());