2011-10-31 6 views
22

と:私はそれは例えばのでORを使用したいQueryOverまたは副問合せ

SELECT * 
FROM item this_ 
WHERE this_.ItemId in (SELECT this_0_.ItemId as y0_ 
          FROM Foo this_0_ 
          WHERE this_0_.AFlag = 1 /* @p0 */) 
and this_.ItemId in (SELECT this_0_.ItemId as y0_ 
          FROM Bar this_0_ 
          WHERE this_0_.AFlag = 1 /* @p0 */) 

NHContext.Session.QueryOver<Item>() 
      .WithSubquery.WhereProperty(x => x.ItemId).In(QueryOver.Of<Foo>().Where(x => x.AFlag).Select(x => x.ItemId)) 
      .WithSubquery.WhereProperty(x => x.ItemId).In(QueryOver.Of<Bar>().Where(x => x.AFlag).Select(x => x.Item)) 
      .Future<Item>(); 

これは、次のSQLを実行します

SELECT * 
FROM item this_ 
WHERE this_.ItemId in (SELECT this_0_.ItemId as y0_ 
          FROM Foo this_0_ 
          WHERE this_0_.AFlag = 1 /* @p0 */) 
or this_.ItemId in (SELECT this_0_.ItemId as y0_ 
          FROM Bar this_0_ 
          WHERE this_0_.AFlag = 1 /* @p0 */) 

私は次のようなことを行うことでCriteriaでそれを行うことができます:

var disjunction = new Disjunction(); 
disjunction.Add(Subqueries.PropertyIn("ItemId", 
    DetachedCriteria.For<Foo>() 
    .SetProjection(Projections.Property("ItemId")) 
    .Add(Restrictions.Eq("AFlag", 1)) 
)); 

しかし、QueryOverで簡単に行うことができ、プロパティ名に文字列を使用しないようにする方法があるのだろうかと思っていました。

ありがとうございました。

答えて

36
あまり一般的論理和のために

(または)私はあなたがSubqueries.WhereProperty<>の代わりに使用する必要があると思うWithSubquery

Session.QueryOver<Item>() 
    .Where(Restrictions.Disjunction() 
     .Add(Subqueries.WhereProperty<Item>(x => x.ItemId).In(QueryOver.Of<Foo>().Where(x => x.AFlag).Select(x => x.ItemId))) 
     .Add(Subqueries.WhereProperty<Item>(x => x.ItemId).In(QueryOver.Of<Bar>().Where(x => x.AFlag).Select(x => x.Item)))) 
    .Future<Item>();