OrderBy descending in Lambda expression?
I know in normal Linq grammar,
orderby xxx descending is very easy, but how do I do this in Lambda expression?
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);
Read more... Read less...
var items = someEnumerable.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
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() is usually not working but with the
Tolist() it will work. There is no need to use
Tolist() like this.
GroupBy(p => p.Nws_ID).ToList().LastOrDefault();