1

2 つの 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();
4

1 に答える 1

2

この質問に対する答えをすでに提供しているいくつかの質問があります。

1 つの方法は、この質問に対する受け入れられた回答からのアプローチを使用することです。

query.Where(Restrictions.On<Type>(x=> x.Foo).IsLike("xpto", MatchMode.Anywhere) ||
        Restrictions.On<Type>(x=> x.Foo).IsLike("abcd", MatchMode.Anywhere))

別のアプローチは、次のことを行うことです。

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.JoinQueryOverJoinAlias を使用してみてください:

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)]
于 2013-04-09T13:58:31.507 に答える