[英]Order by (asc|desc) in linq to SQL Server handles DateTime differently
I am currently trying to add an order by to a LINQ query that will order by a datetime field in an EF object: 我目前正在尝试向LINQ查询添加订单,该查询将按EF对象中的日期时间字段进行排序:
return this.SortingDirection.Equals("asc", StringComparison.InvariantCultureIgnoreCase) ? entities.OrderBy(e => e.ProcessStartTime) : entities.OrderByDescending(e => e.ProcessStartTime);
When the SortingDirection
is set to desc
it works fine. 当
SortingDirection
设置为desc
它可以正常工作。 But when set to asc
I get no records! 但是当设置为
asc
我没有任何记录!
Upon looking at SQL Server Profiler, it turns out that DateTime
objects are being formatted differently! 查看SQL Server Profiler后,发现
DateTime
对象的格式不同!
For DESC
: 对于
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
: 对于
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 swapped, causing the sql query to return no results. 天和月已交换,导致sql查询不返回任何结果。
This to me suggests that the IQueryable.OrderBy()
method is not using the correct local format / different format to OrderByDescending()
, could this be a bug in EF? 对我来说,这表明
IQueryable.OrderBy()
方法未使用正确的本地格式/与OrderByDescending()
不同的格式,这可能是EF中的错误吗?
Is there something in my connection string I could add to enforce this or another way I could sort by these dates? 我可以在连接字符串中添加一些内容以强制执行此操作,还是可以按这些日期排序的另一种方式?
My setup: 我的设置:
Many thanks 非常感谢
You don't show your linq query, but two things come to mind immediately. 您没有显示linq查询,但是立即想到两件事。 First, SQL Server has its own globalization settings, and second, if dates are paramertized (which linq should always do) you shouldn't need to care about date string formats.
首先,SQL Server具有自己的全球化设置,其次,如果日期被参数化(linq应该总是这样做),则无需关心日期字符串格式。
So the problem i'm having has nothing to do with SQL or EF, It turns out Mvc is the problem. 因此,我遇到的问题与SQL或EF无关,事实证明Mvc是问题所在。 I had not set the culture in the
Web.Config
to the Invariant culture ('en'). 我没有在
Web.Config
中将文化设置为不变文化('en')。 This was causing the filter condition (in this case the ProcessorStartTime
) to be parsed as a US date string as .NET seems to default to en-US if no culture is set in the config (see this question ). 这导致过滤条件(在本例中为
ProcessorStartTime
)被解析为美国日期字符串,因为如果在配置中未设置区域性,.NET似乎默认为en-US(请参阅此问题 )。
I did not see this until I tried to sort the result set. 在尝试对结果集进行排序之前,我没有看到它。 We create a new
MvcHtmlString
link on the sort button that included the filter condition for ProcessorStartTime
. 我们在排序按钮上创建了一个新的
MvcHtmlString
链接,其中包括ProcessorStartTime
的过滤条件。 This parsed the date string and reversed the days and months. 这将解析日期字符串,并反转日期和月份。 When I clicked on the link, it gets parsesd back to a
DateTime
as is which trickled down to the LINQ, and therefore causing the sort to return no results (more info on mvc's date handling in this question . 当我单击链接时,它被解析回
DateTime
,并直接向下传递到LINQ,因此导致排序不返回任何结果(有关此问题中 mvc日期处理的更多信息,请参阅。
The reason OrderByDescending()
was working was that the sort button is a toggle bettween off|asc|desc, so it re-reversed days and months back to how they should be! OrderByDescending()
起作用的原因是排序按钮是在off | asc | desc之间切换的开关,因此它又将几天又几个月恢复为原来的样子!
For future reference, make sure that you have got this set in your web config. 为了将来参考,请确保已在Web配置中设置了此设置。 I'll be marking this as a duplicate since its nothing new to SO.
我将其标记为重复,因为它对SO而言并不是什么新鲜事物。 Thank you all for those that posted (esp. you Cylon, it pointed me in the right direction ^^)
谢谢所有发布的人(尤其是Cylon,它为我指明了正确的方向^^)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.