OR-使用ICriteria组合两个条件

时间:2014-04-01 06:43:33

标签: nhibernate icriteria

我想执行一个简单的选择,条件分布在3个表格中。

Select a.*
   FROM TBL_A a
   JOIN TBL_B b ON a.bId=b.Id
   JOIN TBL_C c ON a.cId=c.Id 
   JOIN TBL_D d ON c.dId=d.Id
WHERE (b.value='X' OR c.value='x')
   AND d.someFlag=1

假设这些表已正确映射,如何使用ICriteria API实现此类查询?

到目前为止,我所做的并不能满足所有这些......

1 个答案:

答案 0 :(得分:0)

ICriteria查询可能如下所示:

查询摘要:

 IList<A> list = sess.CreateCriteria(typeof(A), "a")
    .CreateAlias("B", "b")                  // B is property of A a.B
    .CreateCriteria("C", "c")               // C is property of A a.C
    .CreateAlias("D", "d")                  // D is property of C c.D

    .Add( Expression.Disjunction()          // OR statement
       .Add( Expression.Eq("b.value", "X" ) ) // a property "value" from "b" 
       .Add( Expression.Eq("c.value", "x" ) ) // a property "value" from "c"
    )
    .Add( Expression.Eq("d.someFlag", 1) )  // AND this expression

                                            // we are still selecting just a.*
    .SetResultTransformer(Transformers.AliasToBean<A>())
    .List<A>()