码迷,mamicode.com
首页 > Web开发 > 详细

NHibernate 数字类型进行模糊查询

时间:2020-02-25 11:14:17      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:eof   any   and   类型   rop   int   receive   hibernate   expr   

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 数字类型进行模糊查询

标签:eof   any   and   类型   rop   int   receive   hibernate   expr   

原文地址:https://www.cnblogs.com/ulex/p/12360549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!