views:

136

answers:

3

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

A: 

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>();
Jamie Ide
Please don't add new answers; use comments or edit your original question instead. As I said in my answer, it's not clear to me what you're doing with "city." + CitiesProperties.Name.ToString() so I used City.Name instead.
Jamie Ide
A: 

Hi,

thx for your answer. I try but i always get "could not resolve property: City.Name of: WebCrawler.Core.Entities.Advertisements".

senzacionale
A: 

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

senzacionale
try using `CreateAlias<>` for City and Country fields. This allows you to use the alias name to place restrictions on `City.Name` for example
dotjoe
Hi, thx for your answer. can you give me an example please.
senzacionale