如何使用linq或lambda对IEnumerable进行降序排序?
Enumerable.OrderByDescending
如果问题是你想要降序而不是升序
IEnumerable test = new string[] { "abc", "x", "y", "def" };
IEnumerable<string> orderedByLength = test.Cast<string>()
.OrderBy(x => x.Length);
您也可以通过在查询表达式中明确指定类型来实现此操作:
IEnumerable<string> orderedByLength = from string x in test
orderby x.Length
select x;
编辑:现在问题已经澄清,查询表达式格式为:
var query = from value in collection
orderby value.SomeProperty descending
select value;
// Using complex type
class Person()
{
public string Name;
}
IEnumerable<Person> myEnumerable = new List<Person>();
this.myEnumerable.OrderByDescending(person => person.Name)
// Using value type
IEnumerable<int> ints = new List<int>();
ints.OrderByDescending(x => x);