Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>();
like image 584
mxmissile Avatar asked Apr 05 '11 20:04

mxmissile


1 Answers

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.

like image 51
csano Avatar answered Oct 06 '22 04:10

csano