QueryOver 或与子查询 [英] QueryOver Or with Subquery

查看:22
本文介绍了QueryOver 或与子查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下使用子查询的 NHibernate 查询:

IHave the following NHibernate query using a subquery:

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:

This runs the following 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,例如:

I would like it to use OR so for example:

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 中执行以下操作:

I know I can do it in Criteria by doing something like:

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

但是想知道是否有更简单的方法通过 QueryOver 来实现,并且避免使用字符串作为属性名称.

But was wondering if there was an easier way to do it via QueryOver, and avoiding using strings for property names.

感谢您的帮助.

推荐答案

对于不太常见的分离(或)我认为你需要使用 Subqueries.WhereProperty<> 而不是 WithSubquery

For the less common disjunction(or) I think you need to use the Subqueries.WhereProperty<> instead of 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>();

这篇关于QueryOver 或与子查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆