Bitwise And - Bitwise Or Not Supported?

9 posts, 0 answers
  1. Pete Baxter
    Pete Baxter avatar
    7 posts
    Member since:
    Aug 2002

    Posted 10 Jun 2012 Link to this post

    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

  2. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 12 Jun 2012 Link to this post

    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!
  3. DevCraft banner
  4. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 12 Jun 2012 Link to this post

    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!
  5. Pete Baxter
    Pete Baxter avatar
    7 posts
    Member since:
    Aug 2002

    Posted 14 Jun 2012 Link to this post

    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
  6. Pete Baxter
    Pete Baxter avatar
    7 posts
    Member since:
    Aug 2002

    Posted 17 Jun 2012 Link to this post

    Hello. Any news on this?

  7. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 19 Jun 2012 Link to this post

    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.
  8. Pete Baxter
    Pete Baxter avatar
    7 posts
    Member since:
    Aug 2002

    Posted 19 Jun 2012 Link to this post

    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
  9. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 22 Jun 2012 Link to this post

    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.
  10. Pete Baxter
    Pete Baxter avatar
    7 posts
    Member since:
    Aug 2002

    Posted 22 Jun 2012 Link to this post


    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
Back to Top
DevCraft banner