NHibernate instances. Similar criteria for two fields

I have a Nhibernate object that has Firstname and Surname properties, and I would like to be able to query in both fields (Firstname + "+ Surname); for example, If the search term is" John Doe ", this will match when John and Doe are in separate fields.

How can i achieve this? Thanks!

+4
source share
2 answers

So I finished:

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

It seems to work the way I need.

+4
source
 string firstName = "John"; string lastName = "Doe"; 

for example using LINQ:

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

you can do this with QueryOver, which looks pretty much the same:

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

UPDATE: I missed the point.

what about it:

 var name = "John Doe"; Session.Query<User>() .Where(u => name.Contains(u.FirstName) || name.Contains(u.Surname)) .ToList(); 
0
source

Source: https://habr.com/ru/post/1344087/


All Articles