Lambda 表达式中的 OrderBy 降序排列?
我知道在正常的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如 Brannon 所说,它是
OrderByDescending
和ThenByDescending
:相当于:
As Brannon says, it's
OrderByDescending
andThenByDescending
:is equivalent to:
使用 System.Linq.Enumerable.OrderByDescending() 吗?
例如:
Use
System.Linq.Enumerable.OrderByDescending()
?For example:
试试这个:
Try this:
尝试另一种方式:
Queryable.ThenBy
Try this another way:
Queryable.ThenBy
这只适用于您有数字字段的情况,但您可以在字段名称前面放置一个减号,如下所示:
但是,当您运行它时,这与
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:
However this works a little bit different than
OrderByDescending
when you have are running it on anint?
ordouble?
ordecimal?
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.LastOrDefault()
通常不起作用,但使用Tolist()
则可以起作用。不需要像这样使用OrderByDescending
和Tolist()
。LastOrDefault()
is usually not working but with theTolist()
it will work. There is no need to useOrderByDescending
useTolist()
like this.