Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate Expression.Like Criteria on Two Fields

I have an Nhibernate object that has the properties Firstname and Surname, and I'd like to be able to query on both fields (Firstname + " " + Surname); e.g. If the search term is "John Doe", this will be matched when John and Doe are in seperate fields.

How can I achieve that? Thanks!

like image 821
RSlaughter Avatar asked Mar 17 '11 11:03

RSlaughter


1 Answers

So I ended up going with:

.Add(Restrictions.Like(Projections.SqlFunction("concat",
        NHibernateUtil.String,
        Projections.Property("Firstname"),
        Projections.Constant(" "),
        Projections.Property("Surname")),
    searchString, MatchMode.Anywhere))

Which seems to work as I need it to.

like image 128
RSlaughter Avatar answered Sep 27 '22 21:09

RSlaughter