How to use flags enums in Linq to Entities queries?

后端 未结 8 1013
轮回少年
轮回少年 2021-01-01 04:33

I have a [Flags] enum like this:

[Flags]
public enum Status
{
  None = 0,
  Active = 1,
  Inactive = 2,
  Unknown = 4
}

A Status enum may c

相关标签:
8条回答
  • 2021-01-01 05:22

    I am unsure if a bitwise AND-operation will work, but try casting s to an int:

            int i = (int)s;
            var result = from r in db.Records
                 select r
                 where (r.Status & i) == r.Status
    

    Which database engine are you using? Possibly the engine does not support bitwise operations.

    Reference: http://www.matthidinger.com/archive/2008/02/26/entity-framework-comparison-frustration-explained.aspx

    0 讨论(0)
  • 2021-01-01 05:25
    var result = from r in db.Records
                 where r.Status == s
                 select r
    
    0 讨论(0)
提交回复
热议问题