I’m using NHibernate 3.2 and have the following model:
class User
{
public virtual int Id { get; set; }
public virtual string Username { get; set; }
public virtual IList<Log> Logs { get; set; }
}
class Log
{
public virtual int Id { get; set; }
public virtual User User { get; set; }
public virtual DateTime Date { get; set; }
}
Now, I want to query User with the date of ther latest Log-entry.
class UserDto
{
public int UserId { get; set; }
public string Username { get; set; }
public DateTime? LastLogDate { get; set; }
}
What is the most effective way to do this query using NHibernate QueryOver or Linq?
This is the SQL-query I’d like to prodcue:
SELECT u.Id as UserId,
u.Username as Username,
(SELECT TOP 1 l.Date
FROM [Userlog] l
WHERE l.User_id = u.Id) as LastLogDate
FROM [User] u
you could do the same thing with an aggregate query…
or with subquery…