Nhibernate QueryOver 通过枚举标志

发布于 2025-01-07 11:42:42 字数 668 浏览 5 评论 0原文

我通过 QueryOver 进行查询:

public IList<Person> SearchTest(PersonEnumType type)
{
    var q = SessionInstance.QueryOver<Person>();
    q = q.Where(x => (x.PersonEnumType & type) == type);
    return q.List<Person>();
}

并且 PersonEnumType 是一个枚举标志:

[Flags]
public enum PersonEnumType
{
     Employee1 = 1,
     Employee2 = 2,
     Employee3 = 4
}

这会抛出 Could not certain member from (Convert(x.PersonEnumType) & Convert(value(NHibernate.Repository.PersonRepositoryNh+) <>c__DisplayClass2).type))

当然,这在 Nhibernate.Linq 中有效。

为什么?

I have a query by QueryOver :

public IList<Person> SearchTest(PersonEnumType type)
{
    var q = SessionInstance.QueryOver<Person>();
    q = q.Where(x => (x.PersonEnumType & type) == type);
    return q.List<Person>();
}

and PersonEnumType is a Enum flags :

[Flags]
public enum PersonEnumType
{
     Employee1 = 1,
     Employee2 = 2,
     Employee3 = 4
}

This throws Could not determine member from (Convert(x.PersonEnumType) & Convert(value(NHibernate.Repository.PersonRepositoryNh+<>c__DisplayClass2).type))

Of course this works in Nhibernate.Linq.

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十级心震 2025-01-14 11:42:43

如果您已在映射文件中正确映射您的属性:

<property name="PersonEnumType" type="MyApp.PersonEnumType, MyApp">
    <column name="Person" default="1" />
</property>

您可以使用过滤器实现您正在寻找的内容。
我不知道这是否是唯一的解决方案,但是,这里是:

您可以创建过滤器定义:

<filter-def name="PersonEnumTypeFilter">
    <filter-param name="personType" type="MyApp.PersonEnumType, MyApp"/>
</filter-def>

并在类映射中实现它:

<filter name="PersonEnumTypeFilter" condition="(:personType & PersonEnumType) = PersonEnumType"/>

现在您可以打开过滤器:

public IList<Person> SearchTest(PersonEnumType type)
{
    SessionInstance.EnableFilter("PersonEnumTypeFilter").SetParameter("personType",   type);
    var q = SessionInstance.Query<Person>();
    return q.ToList<Person>();
}

您可以阅读有关过滤器的更多信息 此处

if you've mapped your property properly in your mapping file:

<property name="PersonEnumType" type="MyApp.PersonEnumType, MyApp">
    <column name="Person" default="1" />
</property>

You can achieve what you're looking for using filters.
I don't know if this is the only solution but, here it goes:

You can create a filter definition:

<filter-def name="PersonEnumTypeFilter">
    <filter-param name="personType" type="MyApp.PersonEnumType, MyApp"/>
</filter-def>

and implement it in your class mapping:

<filter name="PersonEnumTypeFilter" condition="(:personType & PersonEnumType) = PersonEnumType"/>

Now you can switch on your filter:

public IList<Person> SearchTest(PersonEnumType type)
{
    SessionInstance.EnableFilter("PersonEnumTypeFilter").SetParameter("personType",   type);
    var q = SessionInstance.Query<Person>();
    return q.ToList<Person>();
}

You can read more about filters here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文