&安培;和| NHibernate标准中的运算符

时间:2011-02-07 08:46:46

标签: nhibernate

我需要用NHibernate标准编写类似的东西:

SELECT something FROM sometable
WHERE (something & 1) = 1

其中“something”是int。

我不知道如何使用条件编写WHERE部分:

ICriteria _Criteria = CreateCriteria();
_Criteria.Add(NHibernate.Criterion.Restrictions.Eq("something", ???);

NHibernate有可能吗?如果没有,如果我需要在WHERE部分中使用值,那么是否存在任何替代解决方案?

1 个答案:

答案 0 :(得分:1)

我不知道条件查询是否显式支持按位运算符,但我没有在文档中看到它。

一种选择是使用HQL。请参阅this SO answer,这表明HQL解析器将通过'&'。我实际上没有尝试过这个。

另一个选择是您可以在SQL中定义一个函数来执行按位工作,然后在查询中使用它。这个blog post涵盖了使用NH的本机SQL函数。你可以创建一个类似的函数(根本没有测试过):

CREATE FUNCTION dbo.BitwiseAnd (@something integer, @andedwith integer) RETURNS bit as
        return select @something & @andedwith
相关问题