tags:

views:

171

answers:

2
Advertisements advertisements = NHibernateSession.CreateCriteria(typeof(Advertisements))
                    .Add(Restrictions.Eq(AdvertisementsProperties.Price.ToString(), price))
                    .Add(Restrictions.Eq(AdvertisementsProperties.HollidayDuration.ToString(), hollidayDuration))
                    .Add(Restrictions.Eq(AdvertisementsProperties.Name.ToString(), name))
                    .Add(Restrictions.Eq(AdvertisementsProperties.Description.ToString(), description))
                    //.Add(Restrictions.Eq(AdvertisementsProperties.DepartureDate.ToString(), departureDate))
                    .Add(Restrictions.Eq(AdvertisementsProperties.City.ToString(), city))
                    .Add(Restrictions.Eq(AdvertisementsProperties.Area.ToString(), area))
                    .Add(Restrictions.Eq(AdvertisementsProperties.Country.ToString(), country))
                    .Add(Restrictions.Eq(AdvertisementsProperties.Agency.ToString(), agency))
                    .UniqueResult<Advertisements>();

but city, area or country can be null. How can i compare null in DB with nhibernate?

+2  A: 
public AbstractCriterion EqOrNull(string property, object value) {
    if (value == null)
        return Restrictions.IsNull(property);
    return Restrictions.Eq(property, value);
}

E.g.:

session.CreateCriteria<Advertisements>()
       .Add(EqOrNull(AdvertisementsProperties.City.ToString(), city));

Also see HHH-2951

Mauricio Scheffer
A: 
session.CreateCriteria<Advertisements>()
       .Add(Expression.Or(
                 Expression.Eq("AdvName", "Cool Advertisement"),
                 Expression.IsNull("AdvName"))
        ).List<Advertisements>();
gillyb