How to use flags enums in Linq to Entities queries?

后端 未结 8 1071
轮回少年
轮回少年 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:10

    The folloiwng works for me in C#

        public const StatusTypes ActiveAlert = StatusTypes.Accepted | StatusTypes.Delivered;
    
            int flags = (int)ActiveAlert;
    
            try
            {
                var query = from p in model.AlertsHistory
                            where (p.UserId == clientId
                            && (p.Status.HasValue && (p.Status.Value & flags) != 0))
                            select p;
                var aList = query.ToList();
    
                return (aList);
    
    
            }
            catch (Exception exc)
            {
                log.Error("Exception getting Alerts History for user.", exc);
                throw;
            }
    

提交回复
热议问题