Vetrivel mp
Vetrivel mp

Reputation: 1224

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

Upvotes: 1

Views: 757

Answers (1)

Radim Köhler
Radim Köhler

Reputation: 123881

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

Upvotes: 4

Related Questions