如何编写两个WhereRestrictionOn之间的OR限制?
sessao.QueryOver<Usuario>(() => usuarioAlias)
.WhereRestrictionOn(usr => usr.Name).IsLike(search, MatchMode.Anywhere)
.Inner.JoinQueryOver(() => usuarioAlias.Funcao)
.WhereRestrictionOn(funcao => funcao.Name).IsLike("xpto", MatchMode.Anywhere)
//I want the OR here
.WhereRestrictionOn(funcao => funcao.Name).IsLike("abcd", MatchMode.Anywhere)
.List();
答案 0 :(得分:2)
有几个问题已经提供了这个问题的答案。
一种方法是使用接受this question的答案的方法:
query.Where(Restrictions.On<Type>(x=> x.Foo).IsLike("xpto", MatchMode.Anywhere) ||
Restrictions.On<Type>(x=> x.Foo).IsLike("abcd", MatchMode.Anywhere))
Another approach将执行以下操作:
query.RootCriteria.Add(Restrictions.Or(
Restrictions.On<ObjectModel.Order>(x=> x.Foo).IsLike("xpto", MatchMode.Anywhere),
Restrictions.On<ObjectModel.Order>(x=> x.Foo).IsLike("abcd", MatchMode.Anywhere)));
而不是使用Inner.JoinQueryOver
尝试使用JoinAlias:
Usuario usuarioAlias = null;
Funcao funcaoAlias = null;
var results = sessao.QueryOver<Usuario>(() => usuarioAlias)
.JoinAlias(x => x.funcao, () => funcaoAlias)
.WhereRestrictionOn(usr => usr.Name).IsLike(search, MatchMode.Anywhere)
.Where(
Restrictions.On<Funcao>(x => funcaoAlias.Name)
.IsLike("xpto", MatchMode.Anywhere) ||
Restrictions.On<Funcao>(x => funcaoAlias.Name)
.IsLike("abcd", MatchMode.Anywhere))
.List();
在我的测试项目中,这产生了SQL语句:
SELECT this_.Id as Id0_1_, this_.Name as Name0_1_, funcaoalia1_.Id as Id1_0_,
funcaoalia1_.Name as Name1_0_, funcaoalia1_.usuario_id as usuario3_1_0_
FROM [Usuario] this_ inner join [Funcao] funcaoalia1_ on this_.Id=funcaoalia1_.Id
WHERE this_.Name like @p0
and (funcaoalia1_.Name like @p1 or funcaoalia1_.Name like @p2);
@p0 = '%test%' [Type: String (4000)],
@p1 = '%xpto%' [Type: String (4000)],
@p2 = '%abcd%' [Type: String (4000)]