Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I target an alias from a restriction, with the QueryOver api?

Tags:

nhibernate

As far as I know, the QueryOver api does not allow you reference an alias by name, but rather you use a typed object. How can I add a restriction to my query that targets the alias?

For example, I would like to accomplish something similar to the following:

var query = session.QueryOver<Person>().JoinQueryOver(x => x.Dogs, () => dogAlias);

return query.Where(Restrictions.Disjunction()
                       .Add(Restrictions.Like("Name", searchQuery, MatchMode.Anywhere))
                       .Add(Restrictions.Like("dogAlias.Name", searchQuery, MatchMode.Anywhere)));
like image 988
Jim Geurts Avatar asked Mar 05 '11 15:03

Jim Geurts


People also ask

What is JoinAlias?

JoinAlias is a lambda expression that creates an alias.

What is QueryOver?

QueryOver is a strongly-typed version of Criteria, and is more NHibernate specific. Pretty much anything you can do in ICriteria can be done with QueryOver.


1 Answers

instead of:

Restrictions.Like("dogAlias.Name", searchQuery, MatchMode.Anywhere)

use:

Restrictions.On(() => dogAlias.Name).IsLike(searchQuery, MatchMode.Anywhere)

So, the complete query would become:

var query = session.QueryOver<Person>()
            .JoinQueryOver(x => x.Dogs, () => dogAlias);

return query.Where(Restrictions.Disjunction()
                .Add(Restrictions.On<Person>(p => p.Name).IsLike(searchQuery, MatchMode.Anywhere))
                .Add(Restrictions.On(() => dogAlias.Name).IsLike(searchQuery, MatchMode.Anywhere)));
like image 55
psousa Avatar answered Oct 13 '22 03:10

psousa