Nhibernate Criteria 多个or条件查询

sql: select * from table t where (t.name like ‘%张三%‘ or t.schoolName like ‘%张三%‘ or t.cityname like ‘%张三%‘)

 

用Nhibernate Criteria就可以写成

 var criteria = NHibernateSessionReader.CreateCriteria(typeof(Contract));

            if (!string.IsNullOrEmpty(keyWord))
            {

      criteria.Add(Expression.Like("name", keyWord, MatchMode.Anywhere)
                       || Expression.Like("school", "%" + keyWord + "%")
                       || Expression.Like("cityname", "%" + keyWord + "%")                    
                       || Expression.Like("province", "%" + keyWord + "%")
                       || Expression.Like("UserName", "%" + keyWord + "%")); 

     }

或者

    if (!string.IsNullOrEmpty(keyWord))
            {

     criteria.Add(
                    Restrictions.Or(Expression.Like("name", keyWord, MatchMode.Anywhere),
                    Restrictions.Or(Expression.Like("school", keyWord, MatchMode.Anywhere),
                    Restrictions.Or(Expression.Like("cityname", keyWord, MatchMode.Anywhere),
                    Restrictions.Or(Expression.Like("province", keyWord, MatchMode.Anywhere),
                    Restrictions.Or(Expression.Like("UserName", keyWord, MatchMode.Anywhere),
                    Restrictions.Like("UserName", keyWord, MatchMode.Anywhere)))))));

    }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。