OrderBy descending in Lambda expression?

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

Can lambda functions be templated?

UPDATE 2018: C++20 will come with templated and conceptualized lambdas. The feature has already been integrated into the standard draft. UPDATE 2014: C++14 has been released this year and now provides Polymorphic lambdas with the same syntax as in this example. Some major compilers already implement it. At it stands (in C++11), sadly no. Polymorphic … Read more

Using Java 8’s Optional with Stream::flatMap

Java 9 Optional.stream has been added to JDK 9. This enables you to do the following, without the need of any helper method: Optional<Other> result = things.stream() .map(this::resolve) .flatMap(Optional::stream) .findFirst(); Java 8 Yes, this was a small hole in the API, in that it’s somewhat inconvenient to turn an Optional<T> into a zero-or-one length Stream<T>. … Read more

Sorting a list using Lambda/Linq to objects

This can be done as list.Sort( (emp1,emp2)=>emp1.FirstName.CompareTo(emp2.FirstName) ); The .NET framework is casting the lambda (emp1,emp2)=>int as a Comparer<Employee>. This has the advantage of being strongly typed. If you need the descending/reverse order invert the parameters. list.Sort( (emp1,emp2)=>emp2.FirstName.CompareTo(emp1.FirstName) );