首页 > 系统服务 > 详细

Nhibernate Criteria 多个or条件查询

时间:2014-08-26 22:57:16      阅读:1078      评论:0      收藏:0      [点我收藏+]

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)))))));

    }

Nhibernate Criteria 多个or条件查询

原文:http://www.cnblogs.com/lyeo/p/3938345.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!