是否可以将3个ICriteria与OR语句一起加入而不是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)));
此致
如果我这样做:
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);
我从来没有得到任何结果。我只用
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);
然后工作。但行数不同
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);
你知道为什么吗?我不明白......
此致
答案 0 :(得分:1)
对多个OR限制使用Disjunction限制;相反,使用Junction进行多个AND。我不确定xProperties类的用途,但这应该让你开始:
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>();