在SQL中我可以写
SELECT blah FROM Clients
Where @p1 Like '%'+lastname+'%'
我如何在hibernate中用CreateCriteria表示这个?
我试过s.CreateCriteria<Client>.Add(Restrictions.Where<Client>(c => "something".Contains(c.LastName))
但是得到一个错误
系统。异常:未识别的方法调用:System。字符串:Boolean Contains(System.String)rn at NHibernate.Impl.ExpressionProcessor。ProcessCustomMethodCall (MethodCallExpression MethodCallExpression)
我也试过
s.CreateCriteria<Client>.Add(Restrictions.Where<Client>(c => "something".IndexOf(c.LastName) != -1))
but get
"类型为'TrinityFinance.Data.Entities '的变量'c'。客户端"从作用域引用",但未定义"
@p1 Like '%'+lastname+'%'
和
不一样 lastname Like '%'+@p1+'%'
s.CreateCriteria<Client>().Add(
Restrictions.InsensitiveLike( "LastName", "something", MatchMode.Anywhere))
感谢一位朋友,我已经解决了我的问题。
var searchCriteria = GetSession().CreateCriteria<Client>();
searchCriteria.Add(Expression.Sql(string.Format("'{0}' like '%' + {1} + '%'", p.ClientInputText,p.DbField)));
var results = searchCriteria.List<Client>();
对于不区分大小写的%Like%搜索
Criteria criteria = session.createCriteria(Any.class);
criteria.add(Restrictions.ilike(propertyName, value, MatchMode.ANYWHERE);
criteria.list();