How do I target an alias from a restriction, with the QueryOver api?
As far as I know, the QueryOver api does not allow you开发者_C百科 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)));
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)));
精彩评论