如何在query over中做多级投影?

How to do more than one level projection in query over?

我厌倦了这个

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;}
}

你必须做一个 JOIN 才能做这样的投影:

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

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

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));

您应该使用连接别名在实体之间进行连接

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

有关更多信息,请查看此 URL :What is the difference between JoinQueryOver and JoinAlias?