I'm currently trying to add an order to a LINQ query that will order a datetime field in an EF object:
return this.SortingDirection.Equals("asc", StringComparison.InvariantCultureIgnoreCase) ? entities.OrderBy(e => e.ProcessStartTime) : entities.OrderByDescending(e => e.ProcessStartTime);
If SortingDirection set to desc , it works fine. But when asc , I don't get any records!
Looking at the SQL Server Profiler, it turns out that DateTime objects are formatted differently!
For desc :
ORDER BY [Project1].[StartTime] DESC',N' ...@p __linq__22='2015-01-07 09:00:23',@p__linq__23='2015-01-07 09:00:23',@p__linq__24='2015-01-07 09:05:30',@p__linq__25='2015-01-07 09:05:30'
and for asc :
ORDER BY [Project1].[StartTime] ASC',N' ...@p __linq__22='2015-07-01 09:00:23',@p__linq__23='2015-07-01 09:00:23',@p__linq__24='2015-07-01 09:05:30',@p__linq__25='2015-07-01 09:05:30'
Days and months have been replaced, as a result of which the sql query does not return any results.
This for me suggests that the IQueryable.OrderBy() method does not use the correct local format / other format for OrderByDescending() , could this be an error in EF?
Is there anything in my connection string that I could add to apply this or another way that I could sort by these dates?
My setup:
- .NET 4.5
- Entity Framework 5.0.0
- SQL Server 2012 Standard
Thank you very much
source share