QueryOver或子查询

时间:2011-10-31 18:04:25

标签: nhibernate queryover

我使用子查询获得以下NHibernate查询:

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 */)
and    this_.ItemId in (SELECT this_0_.ItemId as y0_
                            FROM   Bar this_0_
                            WHERE  this_0_.AFlag = 1 /* @p0 */)

我希望使用 OR ,例如:

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来完成它,并避免使用字符串作为属性名称。

感谢您的帮助。

1 个答案:

答案 0 :(得分: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>();