-2

可能重复:
使用 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}},

顺便说一下,这是按“列表”排序的。

谢谢

4

1 回答 1

2

sam这是按属性的第一项排序的 LINQ list(我猜这就是您所追求的,对吧?):

IEnumerable<sample> orderedSam = sam.OrderBy(item => item.list.FirstOrDefault());
于 2012-09-03T02:51:39.767 回答