22

I'm trying to do this :

var list = Session.QueryOver<Person>()
    .Where(x => x.LastName.Contains(searchText))
    .List<Person>();

but I get this error : Unrecognised method call: System.String:Boolean Contains(System.String)

Do you have an idea ?

Update :

public class Person
{
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
}
TheBoubou
  • 19,487
  • 54
  • 148
  • 236

4 Answers4

34

NHibernate does not have direct C# equivalent as mentioned on this link http://nhibernate.info/blog/2009/12/17/queryover-in-nh-3-0.html

Additional Restrictions

Some SQL operators/functions do not have a direct equivalent in C#. (e.g., the SQL where name like '%anna%'). These operators have overloads for QueryOver in the Restrictions class, so you can write:

.Where(Restrictions.On(c => c.Name).IsLike("%anna%"))

There is also an inline syntax to avoid the qualification of the type:

.WhereRestrictionOn(c => c.Name).IsLike("%anna%")

hazzik
  • 13,019
  • 9
  • 47
  • 86
HatSoft
  • 11,077
  • 3
  • 28
  • 43
  • Thanks, that's work. But NHProf warn me about performance trouble (with 'like'). I'll may be keep my current code, a Nhibernate query and after a linq query on the result. – TheBoubou Jul 22 '12 at 16:11
4

Looks like QueryOver doesn't support Contains method. You could try with IsLike restriction:

nhibernate queryover LIKE with expression trees
NHibernate 3.0 search with substring
queryover and (x like 'a' or y like 'a')

Community
  • 1
  • 1
Miroslav Popovic
  • 12,100
  • 2
  • 35
  • 47
2
var data = session.QueryOver<tablename>()    
                  .JoinQueryOver<if another table present>(x => x.Empsals)    
                  .WhereRestrictionOn(x => x.FName).IsLike("a%")        
                  .List<EmployeeDetails>();
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
1
WhereRestrictionOn(x => x.FName).IsLike("a%") use like this
Abhijeet
  • 43
  • 1
  • 10