This is a migrated thread and some comments may be shown as answers.

Bitwise And - Bitwise Or Not Supported?

8 Answers 381 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Pete Baxter
Top achievements
Rank 1
Pete Baxter asked on 11 Jun 2012, 02:14 AM
I have a LINQ statement with a WHERE clause which I have reduced to this:

 

 

where ((t.AgeFilterId == 0) || (((int) t.AgeFilterId & 4) == 4))

 


This generates an exception unhandled in user code. If I remove the "&" operator , this works fine.

I have a SQL smallint field which is a bitflag field that I need to performa a bitwise AND on.

Is this supported in a way different than above? If so how?

Peter

8 Answers, 1 is accepted

Sort by
0
Thomas
Telerik team
answered on 12 Jun 2012, 10:50 AM
Unfortunately, at the moment bitwise operations are not supported yet.
How important is that (database specifc) feature for you, could you go with a numeric calculation instead?


All the best,
Thomas
the Telerik team
Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
0
Thomas
Telerik team
answered on 12 Jun 2012, 10:50 AM
Unfortunately, at the moment bitwise operations are not supported yet.
How important is that (database specifc) feature for you, could you go with a numeric calculation instead?


All the best,
Thomas
the Telerik team
Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
0
Pete Baxter
Top achievements
Rank 1
answered on 14 Jun 2012, 03:47 PM
Can you give me an example of how to use a mathematical expression in place of the bitwise operator? 
Specifically, how do I use math operators to check if the second bit (from the right)? 
Can you show me how to replace this expression (((int) t.AgeFilterId & 4) == 4))

Peter
0
Pete Baxter
Top achievements
Rank 1
answered on 17 Jun 2012, 05:30 AM
Hello. Any news on this?

0
Viktor Zhivkov
Telerik team
answered on 19 Jun 2012, 02:08 PM
Hi Peter,

There is a workaround using the OpenAccess specific extension method SQL<T>().
You can re-work your query like this:
.Where((t.AgeFilterId == 0) || "(({0} & 4) = 4)".SQL<bool>(t.AgeFilterId))

I hope that will be a suitable solution for your problem. If not, please do not hesitate to contact us for further assistance.

Greetings,
Viktor Zhivkov
the Telerik team
OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
0
Pete Baxter
Top achievements
Rank 1
answered on 19 Jun 2012, 06:28 PM
Viktor, 

I was not sure what your reply meant since it did not compile and does not have any instructions on what you had intended. Your reply is not an example of how to perform the bitwise operation using mathematical operators.  Instead, you proposed an extension method to a Lambda expression which can then use the standard .NET bitwise operator.  

To let other users see how OpenAccess can perform a bitwise compare, here is the fragments that need to be created. First, you need an extension method on the type that you want to do a bitwise operation on. Then, use the extension method in the lambda expression.

    static class LinqExtensionMethods

    {

        public static bool IsBitSet(this LC.Global.PatientAge fieldvalue, int bitmask)

        {

            if (((int) fieldvalue & bitmask) == bitmask) return true

            else return false

        }

    }

// use the method above in a standard old lambda expression 

      var templ = context.orm.Templates.Where(t => t.AgeFilterId.IsBitSet (4));

Hope this helps someone else.

Peter
0
Alexander
Telerik team
answered on 22 Jun 2012, 01:04 PM
Hello Peter,

Please excuse my colleague for not clarifying how this solution works.
Indeed, there is a small mistake in the query, the expression in the Where method should be a lambda:
.Where(t => (t.AgeFilterId == 0) || "(({0} & 4) = 4)".SQL<bool>(t.AgeFilterId))

The idea behind this query is that OpenAccess is not currently able to translate bitwise operators from C# code to SQL, and that is why the SQL<T> extension method is used to inject the condition directly as a SQL statement. The actual AgeFilderId value needed to evaluate the condition is passed as parameter. This way the query would be entirely executed on the server, which normally it more optimal than executing it on the client side.
Hope that helps.

All the best,
Alexander
the Telerik team
OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
0
Pete Baxter
Top achievements
Rank 1
answered on 22 Jun 2012, 02:07 PM

Excellent and thank you for the small correction. 

Hopefully other users can see how to use bit operators in the current release. 


Thanks again, 
Peter
Tags
LINQ (LINQ specific questions)
Asked by
Pete Baxter
Top achievements
Rank 1
Answers by
Thomas
Telerik team
Pete Baxter
Top achievements
Rank 1
Viktor Zhivkov
Telerik team
Alexander
Telerik team
Share this question
or