Search code examples
nhibernatequeryover

Using IsLike in NHibernate QueryOver


I know that it gives some solutions for this error already but I dont find out why I got this error. When I load the List without filtered it works.

if (searchString != "") {
            query = _pagedDataQueryProcessor.GetDefaultQuery<Data.Entities.Password>()
                .Where(
                Restrictions.Disjunction()
                    .Add(Restrictions.On<Data.Entities.Password>(x => x.Name).IsLike(searchString))
                    .Add(Restrictions.On<Data.Entities.Password>(x => x.Description).IsLike(searchString))
                    .Add(Restrictions.On<Data.Entities.Password>(x => x.PasswordText).IsLike(searchString))
   );
}

I found out what the problem was

Solution:

if (searchString != "") {
            query = _pagedDataQueryProcessor.GetDefaultQuery<Data.Entities.Password>()
                .Where(
                Restrictions.Disjunction()
                    .Add(Restrictions.On<Data.Entities.Password>(x => x.Name).IsLike("%" + searchString + "%"))
                    .Add(Restrictions.On<Data.Entities.Password>(x => x.Description).IsLike("%" + searchString + "%"))
                    .Add(Restrictions.On<Data.Entities.Password>(x => x.PasswordText).IsLike("%" + searchString + "%"))
);

What did i changed? i didn't had the "%".

Here I want to filter a list but when I wrote something in the input i always got a empty list.

searchstring is the filtered word

data.entities.password is the list on the db

Can someone help me I dont know what i make wrong.


Solution

  • You need to add wildcards or add a second argument to IsLike.

    ...IsLike("%" + searchString + "%"))
    

    You can put the wildcard anywhere in the string, e.g. only at the beginning or only at the end.

    Or

    ...IsLike(searchString, MatchMode.Anywhere)
    

    You can also use MatchMode.Start or MatchMode.End.