计海龄 发表于 2025-5-30 08:09:07

NHibernate Criteria中的And, Or

public IList QueryRelated(TemplateDao[] templates, DataSourceDao[] datasources)
{
    ICriteria criteria = this._session.CreateCriteria(typeof(BoxDao));
    Expression.ICriterion exp = null;
    exp = Expression.Or(Expression.In("Template", templates), Expression.In("Wrapper", templates));
    exp = Expression.Or(exp, Expression.In("DataSource", datasources));
    exp = Expression.And(exp, Expression.In("State", new  EnumState[] { EnumState.Publish, EnumState.Edit }));
    criteria.Add(exp);

    return criteria.List();
}
如果参数templates、datasources都不为null,执行的SQL如下
SELECT ...
FROM CMS_BOX this_ 
WHERE ((this_.TEMP_ID in (?p0) or this_.WRAPPER_ID in (?p1)) or this_.DS_ID in (?p2)) and this_.BOX_STATE in (?p3, ?p4)
假如templates为null,执行的SQL如下
SELECT ... 
FROM CMS_BOX this_ 
WHERE ((1=0 or 1=0) or this_.DS_ID in (?p0)) and this_.BOX_STATE in (?p1, ?p2, ?p3)
对这种组合的And、Or条件,一开始担心null值会引起NHibernate的异常,所以自己判断参数是否为null来确定怎样使用And、Or,写出来的代码很复杂,测试一下发现NHibernate能够处理数组为null的异常情况,因此代码简单多了。
可以看到,当数组为null时,NHibernate使用一个1=0的表达式,这正是我要的效果,因此可以省略null判断,否则还是需要自己处理的。
 
可以使用HQL,不过HQL不支持null参数,参数的个数为0也不行。
public IList QueryRelated(IList templates, IList datasources)
{
    IList states = new List(2);
    states.Add(EnumState.Publish);
    states.Add(EnumState.Edit);

    return this._session.CreateQuery(@"from BoxDao p 
where (p.Template in :templates or p.Wrapper in :templates or p.DataSource in :datasources) and p.State in :states")
        .SetParameterList("templates", templates)
        .SetParameterList("datasources", datasources)
        .SetParameterList("states", states)
        .List();
}
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: NHibernate Criteria中的And, Or