【问题标题】:QueryOver Or with SubqueryQueryOver 或带有子查询
【发布时间】:2013-09-02 02:17:32
【问题描述】:

我有以下使用子查询的 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 来完成,并避免使用字符串作为属性名称。

感谢您的帮助。

【问题讨论】:

    标签: nhibernate queryover


    【解决方案1】:

    对于不太常见的析取(或),我认为您需要使用Subqueries.WhereProperty&lt;&gt; 而不是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>();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多