Page 1 of 1

Nhibernate

Posted: Wed Apr 10, 2013 12:03 am
by Ken
I have two questions related to Nhibernate.

1. how to pass sortexpression to Nhibernate criteria . For instance ,I want to sort by product.createddate desc.

Code: Select all

ICriteria criteria = NHibernateHelper.CreateCriteria<Product>(maximumRows, startRowIndex, sortExpression);
        criteria.Add(Restrictions.Eq("VisibilityId", (byte)CatalogVisibility.Public));
what value I should give to sortExpression. CreatedDate.Desc?

2. I want to get the lastest created time from product table and assign that to a variable. How can I do that with Nhibernate?

Thanks

Re: Nhibernate

Posted: Tue Apr 16, 2013 2:33 am
by mazhar
Ken wrote:I have two questions related to Nhibernate.

1. how to pass sortexpression to Nhibernate criteria . For instance ,I want to sort by product.createddate desc.

Code: Select all

ICriteria criteria = NHibernateHelper.CreateCriteria<Product>(maximumRows, startRowIndex, sortExpression);
        criteria.Add(Restrictions.Eq("VisibilityId", (byte)CatalogVisibility.Public));
what value I should give to sortExpression. CreatedDate.Desc?

2. I want to get the lastest created time from product table and assign that to a variable. How can I do that with Nhibernate?

Thanks
1- sortExpression => CreatedDate DESC
First property name then a space and finally ASC(optional) or DESC for sort direction

2- Try somthing like this

Code: Select all

DateTime createdDate = NHibernateHelper.CreateCriteria<Product>(1, 0, "CreatedDate DESC")
                .SetProjection(NHibernate.Criterion.Projections.Property("CreatedDate"))
                .UniqueResult<DateTime>();