使用 ICriterion 过滤 NHibernate 子类型

发布于 2024-08-15 15:20:33 字数 988 浏览 9 评论 0原文

在我通过向执行的 DetachedCriteria 添加 ICriterion 来访问数据库之前,有什么方法可以在 SubType 字段上过滤 NHibernate 查询吗?

我的代码看起来像这样:

DetachedCriteria detachedCriteria = DetachedCriteria.For(typeof(MyObject));

    ProjectionList projectionList = Projections.ProjectionList();
    projectionList.Add(Projections.SqlProjection("{alias}.SubType as SubType", new string[] { "SubType" }, new IType[] { TypeFactory.GetAnsiStringType(15) }));

    dc.SetProjection(projectionList);
    dc.Add(Expression.Eq("SubType", "MYOBJECT"));

    using(ISession session = ...)

    {

       ICriteria criteria = detachedCriteria.GetExecutableCriteria(session);

    // Blows up because I don't know how to reference the SubType
    // field with an ICriterion (Expression.Eq("SubType", "MYOBJECT"))
       IList list = criteria.List();

    ...

    }

虽然这可能不是实现我的目标的正确方法,但我希望它至少是可能的,因为我不期待必须重构期望/生成 ICriterion 的接口。我也不一定能够访问需要创建 ICriterion 对象的位置附近的会话(但我可以完全控制将使用的各种 NHibernate 字段/表的别名/命名)。

Is there any way I can filter my NHibernate query on the SubType field before I hit the database by adding an ICriterion to my executing DetachedCriteria?

My code looks something like this:

DetachedCriteria detachedCriteria = DetachedCriteria.For(typeof(MyObject));

    ProjectionList projectionList = Projections.ProjectionList();
    projectionList.Add(Projections.SqlProjection("{alias}.SubType as SubType", new string[] { "SubType" }, new IType[] { TypeFactory.GetAnsiStringType(15) }));

    dc.SetProjection(projectionList);
    dc.Add(Expression.Eq("SubType", "MYOBJECT"));

    using(ISession session = ...)

    {

       ICriteria criteria = detachedCriteria.GetExecutableCriteria(session);

    // Blows up because I don't know how to reference the SubType
    // field with an ICriterion (Expression.Eq("SubType", "MYOBJECT"))
       IList list = criteria.List();

    ...

    }

While this may not be the right way to accomplish my goal, I'm hoping it's at least possible because I'm not looking forward to having to refactor my interfaces that expect / produce an ICriterion. I also don't necessarily have access to the session anywhere near where I need to create the ICriterion object (but I have full control over the aliasing/naming of the various NHibernate fields/tables that will be used).

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

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

发布评论

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

评论(1

泛泛之交 2024-08-22 15:20:33

花了相当多的谷歌搜索来找到这个问题的答案,这里是:
http://derek-says.blogspot.com /2008/08/排除-特定-派生-类型-in.html

   ICriteria crit = sess.CreateCriteria(typeof(Mammal));  
   crit.Add( Expression.Not( Expression.Eq("class", typeof(DomesticCat)) ) );  
   List mammals = crit.List();  

Took quite a bit of googling around to find the answer to this, here it is:
http://derek-says.blogspot.com/2008/08/excluding-particular-derived-types-in.html

   ICriteria crit = sess.CreateCriteria(typeof(Mammal));  
   crit.Add( Expression.Not( Expression.Eq("class", typeof(DomesticCat)) ) );  
   List mammals = crit.List();  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文