Lambda 表达式中的 OrderBy 降序排列?

发布于 2024-08-09 07:56:38 字数 66 浏览 2 评论 0原文

我知道在正常的 Linq 语法中,orderby xxx 降序非常简单,但是如何在 Lambda 表达式中执行此操作?

I know in normal Linq grammar, orderby xxx descending is very easy, but how do I do this in Lambda expression?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

瀟灑尐姊 2024-08-16 07:56:38

正如 Brannon 所说,它是 OrderByDescending ThenByDescending

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);

As Brannon says, it's OrderByDescending and ThenByDescending:

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

is equivalent to:

var query = people.OrderByDescending(person => person.Name)
                  .ThenByDescending(person => person.Age)
                  .Select(person => person.Name);
鹿港小镇 2024-08-16 07:56:38

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

例如:

var items = someEnumerable.OrderByDescending();

Use System.Linq.Enumerable.OrderByDescending()?

For example:

var items = someEnumerable.OrderByDescending();
2024-08-16 07:56:38

试试这个:

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);                
}

Try this:

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);                
}
贱贱哒 2024-08-16 07:56:38

尝试另一种方式:

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

Queryable.ThenBy

Try this another way:

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

Queryable.ThenBy

怪我鬧 2024-08-16 07:56:38

这只适用于您有数字字段的情况,但您可以在字段名称前面放置一个减号,如下所示:

reportingNameGroups = reportingNameGroups.OrderBy(x=> - x.GroupNodeId);

但是,当您运行它时,这与 OrderByDescending 有点不同在 int?double?decimal? 字段上。

将会发生的情况是,在 OrderByDescending 上,空值将位于末尾,而使用此方法时,空值将位于开头。如果您想打乱空值而不将数据分成几部分并稍后拼接,那么这很有用。

This only works in situations where you have a numeric field, but you can put a minus sign in front of the field name like so:

reportingNameGroups = reportingNameGroups.OrderBy(x=> - x.GroupNodeId);

However this works a little bit different than OrderByDescending when you have are running it on an int? or double? or decimal? fields.

What will happen is on OrderByDescending the nulls will be at the end, vs with this method the nulls will be at the beginning. Which is useful if you want to shuffle nulls around without splitting data into pieces and splicing it later.

泼猴你往哪里跑 2024-08-16 07:56:38

LastOrDefault() 通常不起作用,但使用 Tolist() 则可以起作用。不需要像这样使用OrderByDescendingTolist()

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

LastOrDefault() is usually not working but with the Tolist() it will work. There is no need to use OrderByDescending use Tolist() like this.

GroupBy(p => p.Nws_ID).ToList().LastOrDefault();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文