OrderBy descending in Lambda expression?

后端 未结 6 1523
梦谈多话
梦谈多话 2020-12-02 05:12

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

6条回答
  •  感情败类
    2020-12-02 06:14

    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.

提交回复
热议问题