我知道在正常的Linq语法中,order xxx降序是非常容易的,但我如何在Lambda表达式中做到这一点?


当前回答

正如布兰农所说,这是先降后降的顺序:

var query = from person in people
            orderby person.Name descending, person.Age descending
            select person.Name;

等价于:

var query = people.OrderByDescending(person => person.Name)
                  .ThenByDescending(person => person.Age)
                  .Select(person => person.Name);

其他回答

正如布兰农所说,这是先降后降的顺序:

var query = from person in people
            orderby person.Name descending, person.Age descending
            select person.Name;

等价于:

var query = people.OrderByDescending(person => person.Name)
                  .ThenByDescending(person => person.Age)
                  .Select(person => person.Name);

使用System.Linq.Enumerable.OrderByDescending () ?

例如:

var items = someEnumerable.OrderByDescending();

试试另一种方式:

var qry = Employees
          .OrderByDescending (s => s.EmpFName)
          .ThenBy (s => s.Address)
          .Select (s => s.EmpCode);

可查询。ThenBy

LastOrDefault()通常不工作,但与Tolist()它将工作。没有必要像这样使用orderbydescent和Tolist()。

GroupBy(p => p.Nws_ID).ToList().LastOrDefault();

试试这个:

List<int> list = new List<int>();
list.Add(1);
list.Add(5);
list.Add(4);
list.Add(3);
list.Add(2);

foreach (var item in list.OrderByDescending(x => x))
{
    Console.WriteLine(item);                
}