如何使用QueryOver api从限制中定位别名?

时间:2011-03-05 15:05:19

标签: nhibernate

据我所知,QueryOver api不允许您按名称引用别名,而是使用类型化对象。如何在针对别名的查询中添加限制?

例如,我想完成类似于以下内容的事情:

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

1 个答案:

答案 0 :(得分:11)

而不是:

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

使用:

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

因此,完整的查询将成为:

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