Sort DateTime field only by Date part

6

I have a property of an entity that is of type DateTime? and in SQL Server it is of type datetime also. I need to make a query that sort by date, but without considering the time, because in that specific view will not show the time. Then sort by code.

Doing two sorts I get with ThenBy , I do not know how to skip the time.

var query = contexto.Contas.OrderBy(c => c.Data).ThenBy(c => c.Codigo);

Suppose this data

  

001 | 12/22/2016 1:25 PM

     

002 | 12/22/2016 11:25 AM

     

003 | 12/22/2016 12:25 PM

Today is displayed like this:

  

002 | 12/22/2016

     

003 | 12/22/2016

     

001 | 12/22/2016

But it should look like this:

  

001 12/22/2016

     

002 12/22/2016

     

003 12/22/2016

    
asked by anonymous 22.12.2016 / 18:00

2 answers

6

Entity Framework 6

Use DbFunctions.TruncateTime >.

contexto.Contas.OrderBy(c => DbFunctions.TruncateTime(c.Data)).ThenBy(c => c.Codigo);

Entity Framework

22.12.2016 / 18:40
1

An alternative would be to give ToList before ordering, thus

var query = contexto.Contas.ToList().OrderBy(c => c.Data.Date).ThenBy(c => c.Codigo);
    
22.12.2016 / 18:34