使用 ICriterion 过滤 NHibernate 子类型
在我通过向执行的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
花了相当多的谷歌搜索来找到这个问题的答案,这里是:
http://derek-says.blogspot.com /2008/08/排除-特定-派生-类型-in.html
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