流畅的 Nhibernate 表达式可在标记的枚举上进行选择
我有一个域实体,它有一个标记的枚举作为属性。标记的枚举是这些实体的目标受众。然后,用户就有了他们应该看到的实体的标记枚举值。我试图找出正确的表达方式来选择满足用户目标受众的实体。
public class File
{
public virtual TargetAudience TargetAudience { get; set; }
}
[Flags]
public enum TargetAudience
{
Audience1 = 1,
Audience2 = 2,
Audience3 = 4,
Audience4 = 8
}
表达式:(这在 IList
上执行时有效,但不适用于对数据库的查询。)
public Expression<Func<File, bool>> Expression
{
get { return ((x.TargetAudience & UserTargetedAudience) > 0); }
}
任何建议都会有帮助。
I have a domain entity that has a flagged enum as a property. The flagged enum is the target audience for the these entities. The user then has a flagged enum value of the entities they should see. I am trying to figure out the correct expression to select entities that meet the target audience for the user.
public class File
{
public virtual TargetAudience TargetAudience { get; set; }
}
[Flags]
public enum TargetAudience
{
Audience1 = 1,
Audience2 = 2,
Audience3 = 4,
Audience4 = 8
}
Expression: (This works when performed on a IList<File>
, but doesn't work on a query to the database.)
public Expression<Func<File, bool>> Expression
{
get { return ((x.TargetAudience & UserTargetedAudience) > 0); }
}
Any suggestions would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在对数据库的查询中它以什么方式不起作用?
您没有显示完整的实现或映射。您是否将
TargetAudience
保留为数字数据类型?除非您跳过一些困难来执行此操作,否则您的枚举将作为文本存储在数据库中,因此您无法对其执行按位操作。(此行为与我在博客等中看到的一些内容相反,所以我不知道 (a) 自以前的版本以来它是否已更改,(b) 它是否对我正在使用的 SQLite 提供程序来说是唯一的,或 (c) 如果 Fluent NHibernate 以这种方式映射。)
您可以执行文本比较。组合标志存储为逗号分隔列表,因此
TargetAudience.Audience4 | TargetAudience.Audience1
将不会保留为9
,而是保留为Audience1, Audience4
。请注意,即使我以相反的顺序指定它们,它也会按升序保留。您可以快速编写一些[扩展]方法来封装进行这些文本比较的麻烦。
编辑:
请参阅 你怎么样使用流畅的 NHibernate 将枚举映射为 int 值? 了解有关将枚举持久化为整数的信息
In what way does it not work in a query to the database?
You aren't showing your full implementation nor your mapping. Are you persisting
TargetAudience
as a numeric datatype?Unless you are jumping through some hoops to do so, your enum will be stored as text in the database, so you can't perform bitwise operations on it. (This behaviour is contrary to some of what I've seen around blogs, etc., so I don't know (a) if it has changed since prior versions, (b) if it is somehow unique to the SQLite provider that I'm using, or (c) if it is mapped this way by Fluent NHibernate.)
You can perform a textual comparison. Combined flags get stored as a comma-separated list, so
TargetAudience.Audience4 | TargetAudience.Audience1
would be persisted not as9
, but asAudience1, Audience4
. Note that it gets persisted in ascending order, even though I specified them in reverse order.You could write a few [extension] methods in short order that would encapsulate the nastiness of doing these textual comparisons.
edit:
See How do you map an enum as an int value with fluent NHibernate? for information about persisting enums as integers
您可以使用 CustomType 方法将枚举属性映射为 int 列。
You can map an enum property as an int column, with method CustomType.