我有这个系列:
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}},
顺便说一句,这就是'列表'。
由于
答案 0 :(得分:2)
以下是sam
属性的第一项命令list
的LINQ(我收集的是你所追求的,对吧?):
IEnumerable<sample> orderedSam = sam.OrderBy(item => item.list.FirstOrDefault());