在我通过向执行的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字段/表的别名/命名)。
答案 0 :(得分:5)
进行了相当多的谷歌搜索,找到了答案,这里是: 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();