4

I'm making a search with NHibernate 3.0 IQueryOver, where I have a keyword for a search. I need to search in a string to see if it is part of the string,

Query().Where(e => e.Name.Contains(keyword)).List();

But this does not do the job as expected. How should such a search be performed?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rasmus Christensen
  • 8,321
  • 12
  • 51
  • 78
  • 1
    Please include what DB you are using and what exactly is not working as expected (not working at all, Exceptions, results that should or should not be includedm, etc.) As Notoriousxl pointed out, SQL CE does not support this. – Florian Lim Mar 28 '11 at 21:47
  • Did you try replacing Contains with 'In'? There are other posts in SO mentioning a similar problem like yours: http://stackoverflow.com/questions/5044851/nhibernate-dynamic-queryover-parameter – WorldIsRound Mar 28 '11 at 22:41

2 Answers2

7

I checked the NHibernate source and the ExpressionProcessor for the QueryOver string like you posted above does not support Contains. The operations it supports are IsLike and IsIn. You could either use IsLike or if you are keen on Contains, use Linq. For example :

(from user in db.Users 
where names.Contains(user.Name)
  select user);

or

query.Where(person.Name.IsLike("%test%")) //In QueryOver

I am guessing that you got an "Unrecognised method call" exception.

WorldIsRound
  • 1,544
  • 10
  • 15
0

As far as I know (at least, for SQL Server, it doesn't seem to work with SQL Server Compact), NHibernate translates that IQueryable into a case insensitive "like '%keywork%'" where clause. Did you expect a case sensitive search?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Notoriousxl
  • 1,540
  • 1
  • 16
  • 27