Ordering nullable DateTime in Linq to SQL

This is a bit of a hack, but it appears to work with Linq to SQL:

return from ju in context.Job_Users_Assigned
          where ju.UserID == user.ID
          orderby ju.Created ?? DateTime.MaxValue descending;

So I’m substituting the maximum possible DateTime value when the actual “Create” value is null. That’ll put all the null values at the top.

Another approach is to order by whether the date field has a value. This works too:

return from ju in context.Job_Users_Assigned
          where ju.UserID == user.ID
          orderby ju.Created.HasValue descending
          orderby ju.Created descending;

Leave a Comment

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