1

我有以下条件查询:

        Criteria criteria = session.createCriteria(Parts.class);
        Iterator<String> iterator = searchList.iterator();
        Disjunction or =  Restrictions.disjunction();
        while (iterator.hasNext()){
            or.add(Restrictions.like("partName", "%"+iterator.next()+"%"));
        }
        criteria.add(or);
        criteria.add(Restrictions.in("partType", partType));
        criteria.addOrder(Order.asc("partDesc"));

这会产生以下 where 子句:

from PartsDb..Parts this_ where (this_.PartName like ? or this_.PartName like ? or this_.PartName like ?) and this_.PartType in (?, ?) order by this_.PartDesc asc

整个 'OR' 子句都在大括号中,它没有给出正确的结果。我希望查询是这样的(在 OR 子句周围没有大括号):

from PartsDb..Parts this_ where this_.PartName like ? or this_.PartName like ? and this_.PartType in (?, ?) order by this_.PartDesc asc

所以我想写这个:

Select * from dbo.Parts 
where 
PartName like '%ABC%'
or PartName like '%XYZ%'
or PartName like '%PQR%'
....
...

and PartType in ('a','b')
...

“searchList”中的对象数量将根据用户输入的内容而有所不同。任何有关如何从大括号中获取可变数量的“OR”子句的帮助将不胜感激!

谢谢

4

0 回答 0