4

我有一个枚举:

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

例如,在一个表中,我们的值等于 3,这意味着 - PasswordRecovery, Activation。这是我的查询:

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

当然这个查询不能正常工作:3 == 1 => false. 如何正确过滤?
谢谢。

更新: 我正在使用Flags属性,因为还有一个使用 XAF(devexpress 框架)编写的应用程序,并且我在 UI 中有一个需要Flags属性的功能。

4

3 回答 3

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

编辑:我添加了一个演员表,因为es.EmailTypeis 和int.

于 2012-06-20T08:31:59.973 回答
2
Database.EmailSettings.FirstOrDefault(
    es => es.EmailType.HasFlag(EmailType.PasswordRecovery));
于 2012-06-20T08:34:29.233 回答
0

I think EmailType == 3 is one of your conditions? Maybe you can get the results using other conditions first, and then filter with this condition in C# code using Enum.HasFlag method. If this is the only condition you want to query, maybe you can store "1,2" instead of "3" in DB.

于 2012-06-20T08:35:41.760 回答