【问题标题】:QueryOver where child not nullQueryOver where child not null
【发布时间】:2013-08-29 09:39:45
【问题描述】:

我正在尝试获得仅在子项不为空时才返回父项的结果。我是 nhibernate 和 QueryOver 语法的新手,如果这完全错误,我很抱歉,但我尝试了这个:

return session.QueryOver<Parent>().Where(x => x.Child != null).SingleOrDefault();

但是,这仍然返回父行。然后我尝试了以下方法:

Child child = null;
return session.QueryOver<Parent>()
.JoinAlias(x => x.Child, () => child)
.Where(() => child.Name != null)
.And(x=>x.Id == id).SingleOrDefault();

仍然没有运气,因为我还是得到了父行。我究竟做错了什么?我很确定我的方法是错误的,只是想不出替代方案。

【问题讨论】:

    标签: c# nhibernate queryover


    【解决方案1】:

    基本查询应该是这样的:

    Parent parentAlias = null;
    Child childAlias = null;
    
    return session.QueryOver<Parent>(() => parentAlias).WithSubquery
          .WhereExists(QueryOver.Of<Child>(() => childAlias)
             .Where(() => parentAlias.Id == childAlias.Parent.Id)
             .Select(c => childAlias.Id))
          .SingleOrDefault();
    

    请注意别名的使用,以及我通过使用子查询解决了您的查询的事实。请注意,即使在子查询中,“连接条件”也必须手动“插入”(我使用过parentAlias.Id == childAlias.Parent.Id

    【讨论】:

      猜你喜欢
      • 2018-11-11
      • 1970-01-01
      • 2014-12-08
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      相关资源
      最近更新 更多