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

    }

posted on 2014-08-26 22:20  裁决  阅读(2141)  评论(0编辑  收藏  举报