Reputation: 20916
Is it possible to join 3 ICriteria together with OR statement not AND?
ICriteria criteriaCity = NHibernateSession.CreateCriteria(typeof(Advertisements))
.CreateCriteria(AdvertisementsProperties.City.ToString(), "city").
Add(Restrictions.Or(
Restrictions.Like("city." + CitiesProperties.Name.ToString(), text, MatchMode.Anywhere),
Restrictions.Like("city." + CitiesProperties.SlovenianName.ToString(), text, MatchMode.Anywhere)));
ICriteria criteriaArea = NHibernateSession.CreateCriteria(typeof(Advertisements))
.CreateCriteria(AdvertisementsProperties.Area.ToString(), "area").
Add(Restrictions.Or(
Restrictions.Like("area." + AreasProperties.Name.ToString(), text, MatchMode.Anywhere),
Restrictions.Like("area." + AreasProperties.SlovenianName.ToString(), text, MatchMode.Anywhere)));
ICriteria criteriaCountry = NHibernateSession.CreateCriteria(typeof(Advertisements))
.CreateCriteria(AdvertisementsProperties.Country.ToString(), "country").
Add(Restrictions.Or(
Restrictions.Like("country." + CountriesProperties.Name.ToString(), text, MatchMode.Anywhere),
Restrictions.Like("country." + CountriesProperties.SlovenianName.ToString(), text, MatchMode.Anywhere)));
Regards
If i try like this:
var criteria= NHibernateSession.CreateCriteria<Advertisements>();
criteria.CreateCriteria(AdvertisementsProperties.City.ToString(), "City");
criteria.CreateCriteria(AdvertisementsProperties.Area.ToString(), "Area");
criteria.CreateCriteria(AdvertisementsProperties.Country.ToString(), "Country");
var dis = Restrictions.Disjunction();
dis.Add(Restrictions.Like("City.Name", text, MatchMode.Anywhere));
dis.Add(Restrictions.Like("City.SlovenianName", text, MatchMode.Anywhere));
dis.Add(Restrictions.Like("Area.Name", text, MatchMode.Anywhere));
dis.Add(Restrictions.Like("Area.SlovenianName", text, MatchMode.Anywhere));
dis.Add(Restrictions.Like("Country.Name", text, MatchMode.Anywhere));
dis.Add(Restrictions.Like("Country.SlovenianName", text, MatchMode.Anywhere));
criteria.Add(dis);
i never get any result. I i use just
var criteria= NHibernateSession.CreateCriteria(); criteria.CreateCriteria(AdvertisementsProperties.City.ToString(), "City"); criteria.CreateCriteria(AdvertisementsProperties.Country.ToString(), "Country");
var dis = Restrictions.Disjunction();
dis.Add(Restrictions.Like("City.Name", text, MatchMode.Anywhere));
dis.Add(Restrictions.Like("City.SlovenianName", text, MatchMode.Anywhere));
dis.Add(Restrictions.Like("Country.Name", text, MatchMode.Anywhere));
dis.Add(Restrictions.Like("Country.SlovenianName", text, MatchMode.Anywhere));
criteria.Add(dis);
then works. But number of rows are different then
var criteria= NHibernateSession.CreateCriteria<Advertisements>();
criteria.CreateCriteria(AdvertisementsProperties.Country.ToString(), "Country");
var dis = Restrictions.Disjunction();
dis.Add(Restrictions.Like("Country.Name", text, MatchMode.Anywhere));
dis.Add(Restrictions.Like("Country.SlovenianName", text, MatchMode.Anywhere));
criteria.Add(dis);
Do you have any idea why? I do not understand...
Regards
Upvotes: 2
Views: 618
Reputation: 49281
Use a Disjunction restriction for multiple OR restrictions; conversely use Junction for multiple ANDs. I'm not sure what the xProperties classes are for, but this should get you started:
var criteria = session.CreateCriteria<Advertisements>();
var dis = Restrictions.Disjunction();
dis.Add(Restrictions.Like("City.Name", text, MatchMode.Anywhere);
dis.Add(Restrictions.Like("City.SlovenianName", text, MatchMode.Anywhere);
dis.Add(Restrictions.Like("Area.Name", text, MatchMode.Anywhere);
dis.Add(Restrictions.Like("Area.SlovenianName", text, MatchMode.Anywhere);
dis.Add(Restrictions.Like("Country.Name", text, MatchMode.Anywhere);
dis.Add(Restrictions.Like("Country.SlovenianName", text, MatchMode.Anywhere);
criteria.Add(dis);
return criteria.List<Advertisements>();
Upvotes: 1