如何在查询中执行多个级别的投影?

时间:2015-02-09 15:14:58

标签: nhibernate queryover

我累了这个

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

但我得到了这个例外: enter image description here

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

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

3 个答案:

答案 0 :(得分:1)

您必须执行JOIN才能进行类似的投影:

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

答案 1 :(得分: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));

答案 2 :(得分:1)

您应该使用Join alias

在实体之间进行连接
respondentSanctionSubquery = 
    respondentSanctionSubquery
        .JoinAlias(x => x.RespondentObj)
        .JoinAlias(resp => resp.IncidentObj)
        .Select(inc => inc.Id);

有关详细信息,请查看以下网址:What is the difference between JoinQueryOver and JoinAlias?