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); eg 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!


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.


string firstName = "John";
string lastName = "Doe";

for example, using LINQ:

Session.Query<User>()
       .Where(u => u.FirstName == firstName || u.Surname == lastName)
       .ToList();

you could do it with QueryOver, which looks almost the same:

Session.QueryOver<User>()
       .Where(u => u.FirstName == firstName || u.Surname == lastName)
       .List();

UPDATE: I missed the point of the question.

what about this:

var name = "John Doe";
Session.Query<User>()
       .Where(u => name.Contains(u.FirstName) || name.Contains(u.Surname))
       .ToList();
链接地址: http://www.djcxy.com/p/37088.html

上一篇: 更快的方式来做这个选择查询

下一篇: NHibernate Expression.Like标准在两个领域