c# - How to do a specific order by in Entity Framework with a mixture of values and nulls -


i using entity framework, , returning cut down collection, using skip , take, based on order using nullable date column. want order list in following order:

  • date less today
  • date of today
  • date of null
  • date in future

how can in 1 db query?

using (var context = new customersdbcontext()) {     var customers = context.customers.orderby(c => c.nextcontactdate).skip(10).take(10); } 

you can order first "priority" defined rules, (only applies equal priorities) use current criteria this:

var customers = context.customers     .orderby(c => c.nextcontractdate == null ? 2:         c.nextcontactdate < datetime.today ? 0 :         c.nextcontactdate > datetime.today ? 3 : 1)     .thenby(c => c.nextcontractdate)     .skip(10).take(10); 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -