Nhibernate Linq & operator RegisterFunction Firebird
I am using NHibernate with Firebird and would like to create a bitwise and operator for a Firebird function bin_and(a, b)
Something like that:
var result = customers.Where(c => (c.StatusValue & 3) > 0);
The above query will result in something like this:
select * from customers where (StatusValue & 3) > 0
Which is not valid in Firebird, the result should be:
select * from customers where bin_and(StatusValue,3) > 0
Is it possible to overwrite this translated result?
Update
By declaring a function, this is possible:
[LinqExtensionMethod("BIN_AND")] public static int BinAnd(int a, int b) { return a & b; }
var result = customers.Where(c => BinAnd(c.StatusValue, 3) > 0);
This works, but I'm looking for a more general way to wigh '&' or '|' operator...
Update:
@ Fédéric:
I wrote my Dialect class like this:
public class MyFirebirdDialect: FirebirdDialect {
public MyFirebirdDialect()
{
// Bitwise operations
RegisterFunction("band", new BitwiseFunctionOperation("bin_and"));
RegisterFunction("bor", new BitwiseFunctionOperation("bin_or"));
RegisterFunction("bxor", new BitwiseFunctionOperation("bin_xor"));
RegisterFunction("bnot", new BitwiseFunctionOperation("bin_not"));
}
}
I had to import BitwiseFunctionOperation.cs as well
If I debug the code I can see that this class is being used as Dialect and I can see that there is a special function for the "band" group which has the value "bin_and" but a request like this
var result = customers.Where(c => (c.StatusValue & 3) > 0);
ends in sql like this:
select * from customers where (StatusValue & 3) > 0
I think the linq parser is not part of it ...
source to share
Are you using the appropriate dialect? FirebirdDialect
correctly defines bitwise and in HQL ( RegisterFunction("band", new BitwiseFunctionOperation("bin_and"));
and linq-to-nhibernate translate &
( ExpressionType.And
) into the corresponding HQL call .
If you are using an older version of NHibernate, you may need to update.
Firebird bitwise operators added with NH-3630 in NHibernate 4.1.
You can try to backup them in your project with a custom dialect derived from FirebirdDialect
and register these additional features as shown in the link above in your custom dialect builder.
But that won't work because NHibernate 4.1 requires several other changes in NHibernate . Perhaps by patching your local copy of NHibernate 3.4 sources you can do this.
source to share