【问题标题】:NHibernate with IQueryOver - create where-condition with subquery and or conditionNHibernate 与 IQueryOver - 使用子查询和或条件创建 where-condition
【发布时间】:2012-07-03 12:24:31
【问题描述】:

当我这样做时

Acc accountAlias = null;
var subQuery = QueryOver.Of<Temp>()
               .Where(x=>x.IsAccepted==null)
               .And(x=>x.Account.Id==accountAlias.Id);

var results = session.QueryOver<Acc>(()=>accountAlias)
              .Where(x=>x.User.Id==65)
              .WithSubquery.WhereExists(subQuery);

这将创建闲置的 sql:

select *
from Accounts a
where a.User_Id=65
and exists (
    select t.Account_Id
    from Temporary_Accounts t
    where t.IsAccepted is null and t.Account_Id=a.Account_Id)

如何添加或条件,NHibernate 将生成闲置的 sql:

select *
from Accounts a
where a.User_Id=65 
and (a.Amount = 100 or exists (
    select t.Account_Id
    from Temporary_Accounts t
    where t.IsAccepted is null and t.Account_Id=a.Account_Id))

【问题讨论】:

    标签: nhibernate subquery criteria queryover icriteria


    【解决方案1】:

    未经测试,但类似 ​​tihs 的东西可能会起作用

    Acc accountAlias = null;
    var subQuery = QueryOver.Of<Temp>()
                   .Where(x => x.IsAccepted == null)
                   .And(x => x.Account.Id == accountAlias.Id);
    
    var results = session.QueryOver<Acc>(()=>accountAlias)
                      .Where(Restrictions.Disjunction()
                         .Add(Subqueries.WhereExists(subQuery))
                         .Add(x => x.Amount == 100))
                      .And(x => x.User.Id = 65)
                      .List();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 2021-07-14
      • 1970-01-01
      • 2023-04-07
      • 2012-03-25
      • 2019-10-12
      相关资源
      最近更新 更多