简体   繁体   English

对SQL Server的LINQ中的(asc | desc)排序以不同方式处理DateTime

[英]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: 我的设置:

  • .NET 4.5 .NET 4.5
  • Entity Framework 5.0.0 实体框架5.0.0
  • SQL Server 2012 Standard SQL Server 2012标准版

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.

相关问题 LINQ与DESC实体顺序 - linq with entity order by desc 如何根据参数通过ASC或DESC在LINQ中的特定列进行排序 - How to OrderBy ASC or DESC a specific column in LINQ according to parameters 如何在addParameter中插入asc / desc以在SQL查询中进行排序 - How to insert asc/desc in addParameter for sorting in SQL query 如何使用lambda或linq命令asc / dsc - how to order asc/dsc with lambda or linq LINQ to Entities使用日期时间的方式有所不同 - LINQ to Entities using datetime differently 如何在LINQ to Entities(Entity Framewok)中编写SQL ORDER BY {value} DESC LIMIT语句? - How to write the SQL ORDER BY {value} DESC LIMIT statement in LINQ to Entities (Entity Framewok)? 使用ORDER BY DESC将SQL DISTINCT转换为LINQ To Entities(将格式化的日期绑定到ASP.NET DropDownList控件) - Convert SQL DISTINCT with ORDER BY DESC to LINQ To Entities (bind formatted date to ASP.NET DropDownList control) .Net 6 Api 基于 columnName 类型和顺序 Asc Desc 的通用查询排序 - .Net 6 Api Generic query sorting based on columnName type and order Asc Desc 如何检查清单 <T> 是否根据其属性排序(升序或降序)? - How to check if a list of <T> has order (asc or desc) or not, according its properties? 排序自定义gridview ASC和DESC - sort custom gridview ASC and DESC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM