2009-12-02 14 views

Respuesta

1

hizo algo como que hace un tiempo.

Pruebe algo como esto.

PropertyProjection projection = Projections.Property("t.ID"); 
PropertyProjection property = Projections.Property("n.Namn"); 
ICriteria criteria = session.CreateCriteria<TagRegistration>("t") 
       .CreateCriteria("Tag","n") 
       .Add(
         Restrictions.Gt(
          Projections.SqlProjection("({alias}.ID & 3) as bitWiseResult", new[] { "bitWiseResult" }, new IType[] { NHibernateUtil.Int32 }) 
         , 0) 
        ) 
       .AddOrder(Order.Desc(Projections.Count(projection))) 
       .AddOrder(Order.Asc(property)) 
       .SetProjection(Projections.GroupProperty(projection), Projections.GroupProperty(property)) 

Nota esta parte de alias {} .ID & 3) donde insertar el valor directamente que no es muy buena pero funciona :)

Usted podría hacerlo mejor si nos fijamos en la prueba proyecto de NHibernate Nhibernate/Criterios y/o AddNumberProjection.cs

pero hay que hacer una subconsulta para devolver Tag inicializado completamente Creo que esta consulta es mejor que hacer en HQL.

Saludos

14

Así es como podría hacerlo con la API criterios:

[Flags] 
enum Bar{ 
    A = 0x01, 
    B = 0x02, 
    C = 0x04 
} 

var criteria = this.Session.CreateCriteria<Foo>() 
      .Add(BitwiseFlags.IsSet("Bar", Bar.A | Bar.C)); 

usando:

public class BitwiseFlags : LogicalExpression 
{ 
    private BitwiseFlags(string propertyName, object value, string op) : 
     base(new SimpleExpression(propertyName, value, op), 
     Expression.Sql("?", value, NHibernateUtil.Enum(value.GetType()))) 
    { 
    } 

    protected override string Op 
    { 
     get { return "="; } 
    } 

    public static BitwiseFlags IsSet(string propertyName, Enum flags) 
    { 
     return new BitwiseFlags(propertyName, flags, " & "); 
    } 
} 

debe generar la salida siguiente cláusula where:

FROM _TABLE 
WHERE (this_.Bar & 5 = 5) 

que debería darle filas que tienen los indicadores Bar.A y Bar.C establecidos (excluyendo todo lo demás). Debería poder usarlo también con conjunción y disyunción.

+0

buena idea, agregaría un método 'NotSet (..)' que debería dar como resultado '(this_.Bar & 5 = 0)' para completarlo – Holly

Cuestiones relacionadas