开发者

NHibernate 3 - How to perform a LIKE on an id/numeric/int

开发者 https://www.devze.com 2023-04-06 16:17 出处:网络
Whilst SQL Server is perfectly comfortable doing: where Id like \'45%\' (id being an int), NH will complain as it will still try to send the compare value into sql as a SqlParameter of type int whe

Whilst SQL Server is perfectly comfortable doing:

where Id like '45%'

(id being an int), NH will complain as it will still try to send the compare value into sql as a SqlParameter of type int when doing:

q.WhereRestrictionOn(cl =&g开发者_如何学Got; cl.CompanyId).IsLike(companyIdFilter)

So how to get round it with the new QueryOver API?


After some digging around and and trial and error, this does the trick:

q.Where(Expression.Like(
   Projections.Cast(
     NHibernateUtil.String,
     Projections.Property<ChangeLog>(cl => cl.CompanyId)), 
   companyIdFilter.Value.ToString(),  
   MatchMode.Start
));
0

精彩评论

暂无评论...
验证码 换一张
取 消