Here's how you could do it with the criteria API:
[Flags]
enum Bar{
A = 0x01,
B = 0x02,
C = 0x04
}
var criteria = this.Session.CreateCriteria<Foo>()
.Add( BitwiseFlags.IsSet( "Bar", Bar.A | Bar.C ) );
using:
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, " & " );
}
}
should generate the following output where clause:
FROM _TABLE
WHERE (this_.Bar & 5 = 5)
which should give you rows that have flags Bar.A and Bar.C set (excluding everything else). You should be able to use it with conjunction and disjunction too.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…