views:

38

answers:

0

Hi,

I have the class with enum property collection:

public enum PropertyType {
    Apartment, Townhouse, AndSoOn
}

public class Company  {
    private readonly ISet<PropertyType> desiredPropertyTypes;

    public virtual ISet<PropertyType> DesiredPropertyTypes {
        get { return desiredPropertyTypes; }
    }
   // other stuff...
}

and the related FNH mapping:

HasMany<GenericEnumMapper<PropertyType>>(x => x.DesiredPropertyTypes)
   .AsSet().Cascade.All()
   .Access.ReadOnlyPropertyThroughCamelCaseField()
   .Element("PropertyType")
   .ForeignKeyConstraintName("FK_SaleAgentCompanyPreference_PropertyTypes")
   .KeyColumn("CompanyId")
   .Table("SaleAgentCompanyPreference_PropertyTypes");

Having that, I need to "Find all companies for the selected property types".

So having an array of selectedTypes, how would I query the enum collection (DesiredPropertyTypes) of the Company to check if it contains elements from the selectedTypes?

The Criteria API is needed as there is a whole bunch of other parameters built dynamically.

Tried to use restriction and variations, but it gives me "NHibernate.QueryException : could not resolve property: dpt of: Model.Company" or other error.

var selectedTypes = new[] { PropertyType.Appartment, PropertyType.Townhouse};
var allFound = session.CreateCriteria<Company>("a")
   .CreateAlias("a.DesiredPropertyTypes", "dpt", JoinType.LeftOuterJoin)
   .Add(
       Restrictions.In("dpt", selectedTypes)
    ).ToList<Company>();

In LINQ I would express it this way:

var allFound = session.Query<Company>()
    .Where(x => x.DesiredPropertyTypes
       .Any(suchThat => selectedTypes.Contains(suchThat))
    ).ToList();

But cannot figure out how to do it using NH Criteria API.

Thanks in advance, Dmitriy.