开发者

& and | operators in NHibernate criteria

开发者 https://www.devze.com 2023-02-09 13:29 出处:网络
I need to write with NHibernate criteria something like: SELECT something FROM sometable WHERE (something & 1) = 1

I need to write with NHibernate criteria something like:

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

where "something" is int.

I开发者_Go百科 don't know how to write the WHERE part with criteria:

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

It is possible in NHibernate? If not, exist any alternative solution if I need work with value in WHERE part?


I don't know if criteria queries support bitwise operators explicitly, but I don't see it in the documentation.

One option is to use HQL. See this SO answer, which suggests the HQL parser will pass through the '&'. I haven't actually tried this.

Another option is that you can define a function in SQL to do the bitwise work and then use it in your query. This blog post covers using native SQL functions from NH. You can make a function like (haven't tested at all):

CREATE FUNCTION dbo.BitwiseAnd (@something integer, @andedwith integer) RETURNS bit as
        return select @something & @andedwith
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号