SELECT valueFROM fooORDER BY (CASE WHEN x IS NULL THEN 1 ELSE 0 END), x
foo.OrderBy(i => i.x == null ? 1 : 0) .ThenBy(i => i.x);
In actual fact, you can use foo.OrderBy(i => i.x == null) .ThenBy(i => i.x); which shaves a few characters off.I have written a useful extension method that you can use as follows:foo.OrderBy(i => i.x) .NullsLast(i => i.x);alternatively you can easily define a extension method that combines the two actions and producesfoo.OrderByNullsLast(i => i.x);You can find the source code at: http://tahirhassan.blogspot.com/2010/06/linq-to-sql-order-by-nulls-last.htmlThanks for the inspiration.
Post a Comment