简体   繁体   English

nhibernate中的按日期排序列按字母顺序排列

[英]Order by date column in nhibernate is working as order by string

I am trying to sorty data using date column but the column is sorting as string not as date. 我正在尝试使用日期列对数据进行排序,但该列排序为字符串而不是日期。 how to solve this problem? 如何解决这个问题呢?

Code: 码:

var projection = Projections.SqlFunction("lower", NHibernateUtil.String,Projections.Property("datecolumn1"));

if(order="desc")queryOver = queryOver.OrderBy(projection).Desc;
else queryOver = queryOver.OrderBy(projection).Asc;

Result:(sorted by ascending) 结果:(按升序排序)

8/7/2012 5:34 AM
11/7/2012 7:21 AM
11/7/2012 7:21 AM
11/7/2012 7:21 AM
11/7/2012 7:21 AM
8/27/2012 9:35 AM

The snippet above shows that the there is a string projection. 上面的代码段显示有一个string投影。 So the result is sorted as a set of string values. 因此,结果将被排序为一组字符串值。 (and that could lead to different results dependent on sql server default DateTime.ToString format). (这可能导致不同的结果依赖于sql server默认的DateTime.ToString格式)。

To sort it by date add a different projection: 要按日期对其进行排序,请添加不同的投影:

var projection = Projections.Property("datecolumn1"));

Now it should be ordered by native SQL Server DateTime type 现在应该按本机SQL Server DateTime类型进行排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM