Session.QueryOver: What does error : variable 'rg' of type ReportingGroup referenced from scope , but it is not defined" mean?

StackOverflow https://stackoverflow.com/questions/18405921

  •  26-06-2022
  •  | 
  •  

문제

This is the query:

 var reportingGroupYears = _session.QueryOver<ReportingGroup>()
                            .Where(x => x.Number == request.ReportingGroupNumber)
                            .Select(rg=> rg.Year.ToString())
                            .List<string>();

I don't understand what is wrong with it. Defining 'rg' as string is not allowed either, as it give message that it would give rg a different meaning than the one defined in parent scope.

도움이 되었습니까?

해결책

In this case, the Select clause is an issue for NHibernate. It expects the property (during the expression tree parsing) to be converted into SELECT statement. But there is a method call: .ToString().

One way how to solve it, could be explicit Projection like this (see doc QueryOver 16.6. Projections)

var reportingGroupYears = session
    .QueryOver<ReportingGroup>()
    .Where(x => x.Number == request.ReportingGroupNumber)
    .Select(Projections.ProjectionList()
        .Add(Projections.Cast(NHibernateUtil.String, Projections.Property("Year"))
        ))
        .List<string>();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top