Bump!
Currently I workaround this using plain SQL as sub-condition (with
'this_.Id' and string-concated enum params which is definitely ugly):
var types = string.Join(", ", criteria.DesiredPropertyTypes.Select(x
=> string.Format("'{0}'", x)));
query.Add(Restrictions.Or(
Restrictions.IsEmpty("a.DesiredPropertyTypes")
,
Expression.Sql(@"exists (select 1 from
SaleAgentCompanyPreference_PropertyTypes adpt
where
adpt.CompanyId = this_.Id and adpt.PropertyType in
({0}))".With(types))
));
On Jul 23, 1:32 pm, dnagir <[email protected]> wrote:
> Hi,
>
> I have the class:
> public enum PropertyType {
> Apartment, Townhouse, AndSoOn
> }
>
> public class Company {
> private readonly Iesi.Collections.Generic.ISet<PropertyType>
> desiredPropertyTypes;
>
> public virtual Iesi.Collections.Generic.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"
>
> var selectedTypes = new[] { PropertyType.Appartment,
> PropertyType.Townhouse};
> var allFound = session.CreateCriteria<Company>("a")
> .CreateAlias("a.DesiredPropertyTypes", "dpt",
> JoinType.LeftOuterJoin)
> .Add(
> Restrictions.InG("dpt", selectedTypes)
> ).ToList<Company>();
>
> Thanks in advance,
> Dmitriy.
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.