如何将其转换为CriteraQuery:
select n
from TagRegistration t
join t.Tag n
where t.Status & :status > 0
order by count(t.ID) desc
, n.Name asc
Run Code Online (Sandbox Code Playgroud)
Bri*_*vez 14
以下是使用条件API执行此操作的方法:
[Flags]
enum Bar{
A = 0x01,
B = 0x02,
C = 0x04
}
var criteria = this.Session.CreateCriteria<Foo>()
.Add( BitwiseFlags.IsSet( "Bar", Bar.A | Bar.C ) );
Run Code Online (Sandbox Code Playgroud)
使用:
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, " & " );
}
}
Run Code Online (Sandbox Code Playgroud)
应该生成以下输出where子句:
FROM _TABLE
WHERE (this_.Bar & 5 = 5)
Run Code Online (Sandbox Code Playgroud)
这应该给你带有标志Bar.A和Bar.C设置的行(不包括其他所有内容).你应该能够将它与连接和分离一起使用.
前段时间做了类似的事情。
尝试这样的事情。
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))
Run Code Online (Sandbox Code Playgroud)
请注意这部分 {alias}.ID & 3) 我直接插入了值,这不是很好,但它可以工作:)
如果你看看 NHibernate 的测试项目 Nhibernate/Criteria/AddNumberProjection.cs 你可以做得更好
但是你需要执行一个子查询来返回完全初始化的标签,我认为这个查询最好在 Hql 中执行。
问候
| 归档时间: |
|
| 查看次数: |
2137 次 |
| 最近记录: |