Search text contains with QueryOver

Kris-I picture Kris-I · Jul 22, 2012 · Viewed 13k times · Source

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; }
}

Answer

HatSoft picture HatSoft · Jul 22, 2012

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%")