mxmissile
mxmissile

Reputation: 11672

Nhibernate QueryOver Enum Flags

Trying to use QueryOver and a flagged enum query. This works in Nhibernate.Linq:

var results = repo.Query()
  .Where(x => (x.Classification & LineItemClassification.Shipping) == LineItemClassification.Shipping);

This throws Could not determine member from (Convert(x.Classification) & 2) using QueryOver:

 var results = repo.QueryOver()
   .Where(x => (x.Classification & LineItemClassification.Shipping) == LineItemClassification.Shipping);

Any ideas? Suggestions?

Enum:

[Flags]
public enum LineItemClassification
{
        Foo,
        Widget,
        Shipping
}

Mapping:

Map(x => x.Classification)
  .CustomType<LineItemClassification>();

Upvotes: 6

Views: 1358

Answers (1)

csano
csano

Reputation: 13696

I ran into a similar issue today and ended up doing a SQL projection. Not ideal, as it moves us away from the type safety that we get with the QueryOver API, but it works. The relevant portion of my code follows.

.QueryOver<ProjectActivity>()
.Where(Expression.Gt(Projections.SqlProjection(String.Format("({{alias}}.ProjectActivityTypeId & {0}) as ProjectActivityTypeId", (int)type), null, null), 0))

Hope that helps.

Upvotes: 3

Related Questions