简体   繁体   中英

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. 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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