views:

391

answers:

2
criteriaCount.CreateCriteria(AdvertisementsProperties.City.ToString())
                .Add(Expression.Like(CitiesProperties.Name.ToString(), query, MatchMode.Anywhere))
                .Add(Expression.Like(CitiesProperties.SlovenianName.ToString(), query, MatchMode.Anywhere))
                .CreateCriteria(AdvertisementsProperties.Country.ToString())
                .Add(Expression.Like(CountriesProperties.Name.ToString(), query, MatchMode.Anywhere))
                .Add(Expression.Like(CountriesProperties.SlovenianName.ToString(), query, MatchMode.Anywhere));

This return "name like %foo% and slovenianName like %foo% and name like %foo% and slovenianName like %foo%"

but i would like to get "name like %foo% or slovenianName like %foo% or name like %foo% or slovenianName like %foo%"

i can use Expression.Disjunction() for OR but i have problem that i can not use CreateCriteria inside Expression.Disjunction(). Can someone tell me how can i use OR and CreateCriteria together?

Regards

A: 

Use CreateAlias outside the Disjunction().

var result = session.CreateCriteria<Property>()
    .CreateAlias("Cities", "city")
    .CreateAlias("Countries", "country")
    .Add(Restrictions.Disjunction()
        .Add(Expression.Like("city.Name", query, MatchMode.Anywhere))
        .Add(Expression.Like("city.SlovenianNam", query, MatchMode.Anywhere))
        .Add(Expression.Like("country.Name", query, MatchMode.Anywhere))
        .Add(Expression.Like("country.SlovenianNam", query, MatchMode.Anywhere))
    ).List();

The corresponding entites are below. Hopefully they resemble yours.

class Property
{
    public virtual Guid Id { get; set; }
    public virtual IList<City> Cities { set; get; }
    public virtual IList<Country> Countries { set; get; }
}

class City
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string SlovenianNam{ get; set; }
}

class Country
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string SlovenianNam{ get; set; }
}
Lachlan Roche
A: 

Hi, thx for your answer. i try but i get this error: could not resolve property: Area of: Project.Core.Entities.Cities

Then i try like this but is the same:

criteria.CreateCriteria(AdvertisementsProperties.City.ToString(), "city")
    .CreateCriteria(AdvertisementsProperties.Area.ToString(), "area")
    .CreateCriteria(AdvertisementsProperties.Country.ToString(), "country")
    .Add(
        Restrictions.Disjunction()
            .Add(Restrictions.Like("city." + CitiesProperties.Name.ToString(), query, MatchMode.Anywhere))
            .Add(Restrictions.Like("city." + CitiesProperties.SlovenianName.ToString(), query, MatchMode.Anywhere))
            .Add(Restrictions.Like("area." + CitiesProperties.Name.ToString(), query, MatchMode.Anywhere))
            .Add(Restrictions.Like("area." + CitiesProperties.SlovenianName.ToString(), query, MatchMode.Anywhere))
            .Add(Restrictions.Like("country." + CountriesProperties.Name.ToString(), query, MatchMode.Anywhere))
            .Add(Restrictions.Like("country." + CountriesProperties.SlovenianName.ToString(), query, MatchMode.Anywhere))); 

can you suggest me what i am doing wrong. This code works, but Restrictions.Or accept only two parameters not 3.

criteriaCount.CreateCriteria(AdvertisementsProperties.City.ToString(), "city")
.CreateCriteria(AdvertisementsProperties.Country.ToString(), "country")
.Add(
    Restrictions.Or(
        Restrictions.Like("city." + CitiesProperties.Name.ToString(), query, MatchMode.Anywhere) ||
        Restrictions.Like("city." + CitiesProperties.SlovenianName.ToString(), query, MatchMode.Anywhere),
        Restrictions.Like("country." + CountriesProperties.Name.ToString(), query, MatchMode.Anywhere) ||
        Restrictions.Like("country." + CountriesProperties.SlovenianName.ToString(), query, MatchMode.Anywhere)));

regards

senzacionale