Nhibernate Criteria 多个or条件以及Like查询
Nhibernate Criteria 多个or条件以及Like查询
添加引用:using NHibernate.Criterion;
//创建查询条件集合
IList<ICriterion> query = new List<ICriterion>();
1. 调用Expression.Or
query.Add(Expression.Or(Expression.Like("UserName", whereTxt.ToLower(), MatchMode.Anywhere), Expression.Like("Account", whereTxt.ToLower(), MatchMode.Anywhere)));
2. 调用Restrictions.Or
query.Add(Restrictions.Or(Expression.Like("UserName", whereTxt.ToLower(), MatchMode.Anywhere), Expression.Like("Account", whereTxt.ToLower(), MatchMode.Anywhere)));
3. 直接使用||符号连接
query.Add(Expression.Like("UserName", whereTxt.ToLower(), MatchMode.Anywhere) || Expression.Like("Account", whereTxt.ToLower(), MatchMode.Anywhere));
对MatchMode介绍一下。
public abstract class MatchMode
{
//表示 %参数%
public static readonly MatchMode Anywhere;
//表示 参数%
public static readonly MatchMode End;
//表示 参数
public static readonly MatchMode Exact;
//表示 %参数
public static readonly MatchMode Start;
protected MatchMode(int intCode, string name);
public abstract string ToMatchString(string pattern);
public override string ToString();
}
MatchMode.START:字符串在最前面的位置.相当于”like ‘key%’”
MatchMode.END:字符串在最后面的位置.相当于”like ‘%key’”
MatchMode.ANYWHERE:字符串在中间匹配.相当于”like ‘%key%’”
MatchMode.EXACT:字符串精确匹配.相当于”like ‘key’”