【问题标题】:How to do more than one level projection in query over?如何在查询中进行多级投影?
【发布时间】:2015-02-10 22:04:52
【问题描述】:

我厌倦了这个

respondentSanctionSubquery = respondentSanctionSubquery.Select(x => x.Respondent.Incident.Id);

但我得到了这个例外:

我有 3 个实体而不是 2 个实体:

class Respondent
{
public IncidentObj{get;set;}
}
class Incident
{
public int Id{get;set;}
}
class RespondentSanction
{
public Respondent RespondentObj{get;set;}
}

【问题讨论】:

    标签: nhibernate queryover


    【解决方案1】:

    您还必须将其他实体加入到主查询中(如下所示),

    X x = null; 
    Respondent respondent = null;
    Incident incident = null;
    
    respondentSanctionSubquery = respondentSanctionSubquery
            .JoinQueryOver(() => x.Respondent , () => respondent)
            .JoinQueryOver(() => respondent.Incident , () => incident )
            .Select(r => incident.Id);
    

    否则您可能想使用子查询,

    X x = null; 
    Respondent respondent = null;
    Incident incident = null;
    
        var subQuery = (QueryOver<Respondent>)session.QueryOver<Respondent>(() => respondent)
                      .JoinQueryOver(() => respondent.Incident , () => incident )
                      .Where(() => respondent.Id == x.Respondent.Id)
                      .Select(r => incident.Id);
    
        var query = session.QueryOver(() => x)
                    .SelectList(l => l.SelectSubQuery(subQuery));
    

    【讨论】:

    • 我试过这个,但我得到了这个异常'子查询返回超过 1 个值。当子查询跟随 =、!=、、>= 或子查询用作表达式时,这是不允许的。 '
    • 显然您每个 x 有多个事件,这就是您收到此错误的原因,因此您可能应该选择第一个选项。
    • 你能再看看这个问题吗?我对它做了一些修改。
    • 我也厌倦了第一个解决方案,但出现了另一个问题。我收到此异常“无法解析属性:Entities.Incident 的开始日期”请注意,开始日期存在于 RespondentSanction 实体中。请提供任何建议?
    • 您确定您的映射正常吗?听起来像是开始日期映射的问题。
    【解决方案2】:

    您必须使用JOIN 才能进行这样的投影:

    respondentSanctionSubquery = 
        respondentSanctionSubquery
            .JoinQueryOver(x => x.RespondentObj)
            .JoinQueryOver(resp => resp.IncidentObj)
            .Select(inc => inc.Id);
    

    【讨论】:

    • 这仅适用于两个实体,但我有 3 个相互关联的实体,请查找更新后的问题。
    • 也请不要我在子查询中尝试这样做。(这个响应者SanctionSubquery是子查询)。
    • 其实我厌倦了这个但它不起作用,但我找到了我使用的解决方案 .JoinAlias 而不是 JoinQueryOver。
    • 无论如何,安德鲁感谢您的帮助。感谢您的帮助
    • 现在查询无法解析 RespondentSanction 属性
    【解决方案3】:

    你应该在实体之间使用 Join alias 进行连接

    respondentSanctionSubquery = 
        respondentSanctionSubquery
            .JoinAlias(x => x.RespondentObj)
            .JoinAlias(resp => resp.IncidentObj)
            .Select(inc => inc.Id);
    

    欲了解更多信息,请查看此网址:What is the difference between JoinQueryOver and JoinAlias?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      相关资源
      最近更新 更多