How to order by column with null values last in entity framework

I would do:

using(var context = new DomainEntities())
{
    var result = context.Users.OrderBy(u => u.LastName == null)
                              .ThenBy(u => u.LastName)
                              .ThenBy(u => u.FirstName == null)
                              .ThenBy(u => u.FirstName);
}

…which should produce reasonable SQL.

EDIT: explanation (taken from Craig’s comment):

Because false sorts before true.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)