NHibernate 数字类型进行模糊查询

Posted yooric

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NHibernate 数字类型进行模糊查询相关的知识,希望对你有一定的参考价值。

I have a NHibernate search function where I receive integers and want to return results where at least the beginning coincides with the integers, e.g.

received integer: 729
returns: 729445, 7291 etc.

The database column is of type int, as is the property "Id" of Foo.

But

int id = 729;

var criteria = session.CreateCriteria(typeof(Foo))

criteria.Add(NHibernate.Criterion.Expression.InsensitiveLike("Id", id.ToString() + "%"));

return criteria.List<Foo>();

does result in an error (Could not convert parameter string to int32). Is there something wrong in the code, a work around, or other solution?

int id = 729;

var criteria = session.CreateCriteria(typeof(Foo))

criteria.Add(NHibernate.Criterion.Expression.InsensitiveLike("Id", id.ToString() + "%"));

return criteria.List<Foo>();

 


How about this:

int id = 729;

var criteria = session.CreateCriteria(typeof(Foo))
criteria.Add(Expression.Like(Projections.Cast(NHibernateUtil.String, Projections.Property("Id")), id.ToString(), MatchMode.Anywhere));

return criteria.List<Foo>();

 

以上是关于NHibernate 数字类型进行模糊查询的主要内容,如果未能解决你的问题,请参考以下文章

在 NHibernate 中动态更改查询类型

sql语句对int类型进行模糊查询

SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper

如何查询 NHibernate 的特定类型?

mybatis模糊查询

在ORACLE中怎么对LONG类型字段的模糊查询