Nhibernate QueryOver Enum Flags
Asked Answered
B

1

6

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>();
Baryon answered 5/4, 2011 at 20:14 Comment(3)
Does casting x.Classification to an int work?Inboard
Any luck figuring this out? I'm in a similar situation.Comment
@Baryon I have exactly same problem. What do you do for this problem?Slippery
C
3

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.

Comment answered 3/5, 2011 at 22:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.