Search text contains with QueryOver
Asked Answered
S

4

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; }
}
Spearman answered 22/7, 2012 at 15:36 Comment(0)
S
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%")

Sofiasofie answered 22/7, 2012 at 15:47 Comment(1)
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.Spearman
O
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')

Outburst answered 22/7, 2012 at 15:52 Comment(0)
T
2
var data = session.QueryOver<tablename>()    
                  .JoinQueryOver<if another table present>(x => x.Empsals)    
                  .WhereRestrictionOn(x => x.FName).IsLike("a%")        
                  .List<EmployeeDetails>();
Thermometry answered 10/3, 2014 at 13:44 Comment(0)
Z
1
WhereRestrictionOn(x => x.FName).IsLike("a%") use like this
Zephaniah answered 10/3, 2014 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.