Search code examples
nhibernatenhibernate-criterianhibernate-projections

Filtering only negative values using nhibernate projection queries


I am trying to filter only negative values using nhibernate projection queries. Below is my code for it

SearchTemplate RefundTemplate = new SearchTemplate();
            RefundTemplate.Criteria = DetachedCriteria.For(typeof(AirBilling), "Ab");
            RefundTemplate.Criteria.Add(NHibernate.Criterion.Expression.Eq("PaymentType", "CK"));
            RefundTemplate.Criteria.Add(NHibernate.Criterion.Restrictions.Lt("Gross",0));

Basically I am tying to get all the records from the AirBilling table that have a PaymentType CK and Gross value is less that zero. But somehow the code doesnt work. It doesnt throw any errors, but it simply wont work.


Solution

  • It could be because you are giving your entity an alias ("Ab"), so when you reference the properties you need to prefix them with the alias, eg: Expression.Eq("Ab.PaymentType", "CK")

    try this:

    SearchTemplate RefundTemplate = new SearchTemplate();
    RefundTemplate.Criteria = DetachedCriteria.For(typeof(AirBilling), "Ab");
    RefundTemplate.Criteria.Add(NHibernate.Criterion.Expression.Eq("Ab.PaymentType", "CK"));
    RefundTemplate.Criteria.Add(NHibernate.Criterion.Restrictions.Lt("Ab.Gross",0));