Search code examples
c#linqlinq-to-sql

linq-To-Sql: filter with enum(flags)


I have a enum:

[Flags]
public enum EmailType
{
    PasswordRecovery = 1,
    Activation = 2,
    SendTicket = 4
}

For example, in a table we have value equals 3 that means - PasswordRecovery, Activation. This is my query:

var emailType = EmailType.PasswordRecovery;
return Database.EmailSettings.FirstOrDefault(es => es.EmailType ==  (int)emailType);

Of course this query not works properly:3 == 1 => false. How to filter properly?
Thanks.

UPDATE: I'm using Flags attribute because there is one more application written with XAF (devexpress framework) and I have one feature in UI that required Flags attribute.


Solution

  • int emailType = (int)EmailType.PasswordRecovery;
    return Database.EmailSettings.FirstOrDefault(es => (es.EmailType & emailType) == emailType);
    

    Edit: I added a cast since es.EmailType is and int.