Search code examples
c#linqsqlitelambdaormlite

Bitwise flag in ORMLite


I have a bitwise flag column in my db table and I wanna select rows that does not have a certain flag. For example I want to retrieve list of customers that are not deactivated.

I write this in ORMLite

var q = db.Select<Customer>().where(c => (c.Flags & CustomerFlag.Deactive) != CustomerFlag.Deactive);

But when I watch the query that ORMLite generates in debug, '&' turns to 'AND'. So I can't get what I want.

I also used HasFlag. But It seems it's not supported by ORMLite.

Does anybody have a solution?

p.s: DB is SQLite


Solution

  • I hate to answer my own question. But it seems that I finally found an answer. I write it down to help if anyone faced the same issue.

    Actually I replace the 'bitwise' part of lambda with a raw sql codition:

    var q = db.Select<Customer>().Where($"(Flags & {(int)CustomerFlag.Deactive} != {(int)CustomerFlag.Deactive}");
    

    So the generated query is like:

    select * from customer where (flag & 8) <> 8