I was wondering how to do the following in QueryOver (NHibernate 4.0.4)
Say I have this set of classes
class Student
{
public virtual long Id { get; set; }
public virtual IList<Exam> Exams { get; set; }
}
class Exam
{
public virtual string Subject { get; set; }
public virtual int Score { get; set; }
//Many more unneeded properties
}
class StudentDto
{
public long Id { get; set; }
public IList<string> Subjects { get; set; }
public IList<int> Scores { get; set; }
}
How would I go about getting all students along with their Subjects and Scores without fetching the whole entities?
The functionality I am going for is:
foreach(var exam in student.Exams)
{
dto.Subjects.Add(exam.Subject);
dto.Scores.Add(exam.Score);
}
But hopefully using some of NHibernate's functionality like SelectList
Basically what I am trying to do is something along these lines:
StudentDto dto = null;
Student student = null;
Exam exam = null;
QueryOver<Student>(() => student)
.SelectList(list => list
.Select(st => st.Exams.Select(x => x.Subject)).WithAlias(() => dto.Subjects)
.Select(st => st.Exams.Select(x => x.Score)).WithAlias(() => dto.Scores)
)
I've tried the above, I get an exception that the object is out of scope
QueryOver<Student>(() => student)
.SelectList(list => list
.Select(() => student.Exams.Select(x => x.Subject)).WithAlias(() => dto.Subjects)
.Select(() => student.Exams.Select(x => x.Score)).WithAlias(() => dto.Scores)
)
I've tried this, throws a null reference exception, I've also tried the above with a ternary operator to check for whether or not the lists are null and pass an empty list if so, didn't work
EDIT
I would like to return a list of StudentDtos with every one of them containing a list of the respective student's Scores and Subjects.
So a structure somewhat resembling this
StudentDto
-> long ID
-> List<string> subjects -> Math
-> Physics
-> etc..
-> List<int> scores -> 100
-> 94
-> etc..
That's what JoinAlias is for. If the goal is to get students by some filter then this might look like this