I have a 1 to many relation between user 1 =====> * Enrollment

so something like
User
{
     public string UserName;
     public IList<Enrollment> Enrollments;
}

and Enrollment have a CreateOn property

so

Enrollment
{
    public DateTime CreateOn;
}

I would like to pull a list of User and project it to a list UserReport
where
UserReport
{
    public string UserName;
    public int EnrollmentCount;
    public DateTime? LastEnrollment;
}

to do it in Linq would look something like this

            var query = from user in UserRepository
                        select new UserReport ()
                        {
                            UserName = user.UserName,
                            EnrollmentCount = user.Enrollments.Count,
                            LastEnrollment = user.Enrollments
                                .OrderByDescending(enrollment =>
enrollment.CreateOn).
                                FirstOrDefault()
                        };
in fact that's how I currently do it. Pull the list of user and do this in
memory using Linq, but that mean I have to pull all the enrollments of every
users, extremely inefficient.

How do I do this with HQL, Criteria or QueryOver? I think I have to do some
projection... but how is beyond me.

So far I got this far
With HQL

select new UserReport(u.UserName) from User u; <--- though where do I add a
subquery that would give me the last enrollment
from u.Enrollments?

With Criteria I think I should create two criteria... one to select the last
enrollment and then join it with the main query but how is still beyond me.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to