开发者

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

开发者 https://www.devze.com 2023-02-14 20:04 出处:网络
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?

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)));
0

精彩评论

暂无评论...
验证码 换一张
取 消