nhibernate CreateCriteria wildcard Like when

て烟熏妆下的殇ゞ 提交于 2019-12-07 22:58:32

问题


In SQL I can write

SELECT blah FROM Clients Where @p1 Like '%'+lastname+'%'

How do I represent this with CreateCriteria in Nhibernate?

I've tried s.CreateCriteria<Client>.Add(Restrictions.Where<Client>(c => "something".Contains(c.LastName))

but get an error

System.Exception: Unrecognised method call: System.String:Boolean Contains(System.String)\r\n at NHibernate.Impl.ExpressionProcessor.ProcessCustomMethodCall(MethodCallExpression methodCallExpression)

I've also tried

s.CreateCriteria<Client>.Add(Restrictions.Where<Client>(c => "something".IndexOf(c.LastName) != -1))

but get

"variable 'c' of type 'TrinityFinance.Data.Entities.Client' referenced from scope '', but it is not defined"

Note the order is important here.

@p1 Like '%'+lastname+'%'

is not the same as

lastname Like '%'+@p1+'%'


回答1:


s.CreateCriteria<Client>().Add(
      Restrictions.InsensitiveLike( "LastName", "something", MatchMode.Anywhere))



回答2:


Thanks to a friend I've solved my issue.

var searchCriteria = GetSession().CreateCriteria<Client>(); searchCriteria.Add(Expression.Sql(string.Format("'{0}' like '%' + {1} + '%'", p.ClientInputText,p.DbField)));

var results = searchCriteria.List<Client>();




回答3:


For Case-Insensitive %Like% Search

 Criteria criteria = session.createCriteria(Any.class);
 criteria.add(Restrictions.ilike(propertyName, value, MatchMode.ANYWHERE);
 criteria.list();


来源:https://stackoverflow.com/questions/8728796/nhibernate-createcriteria-wildcard-like-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!