2

有什么方法可以在我通过向执行的 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 字段/表的别名/命名)。

4

1 回答 1

5

花了相当多的谷歌搜索找到答案,这里是: http ://derek-says.blogspot.com/2008/08/ exclude-particular-derived-types-in.html

   ICriteria crit = sess.CreateCriteria(typeof(Mammal));  
   crit.Add( Expression.Not( Expression.Eq("class", typeof(DomesticCat)) ) );  
   List mammals = crit.List();  
于 2010-11-04T14:31:55.270 回答