Search text contains with QueryOver

冷暖自知 提交于 2019-12-03 06:56:11

问题


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

回答1:


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




回答2:


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')




回答3:


var data = session.QueryOver<tablename>()    
                  .JoinQueryOver<if another table present>(x => x.Empsals)    
                  .WhereRestrictionOn(x => x.FName).IsLike("a%")        
                  .List<EmployeeDetails>();



回答4:


WhereRestrictionOn(x => x.FName).IsLike("a%") use like this


来源:https://stackoverflow.com/questions/11601590/search-text-contains-with-queryover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!