Given the following tables:
Resources
ID int
Title varchar (100),
Address Varchar (500),
City varchar (100),
and etc.
ResourceViews:
Id int,
resourceId int,
userId int,
viewDate DateTime
every time a resource is viewed, a record is added to the ResourceView table for this user.
Here are the relevant classes:
public class Resource { public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } public string City { get; set; } public IList<ResourceView> ResourceViews { get; set; } // simplified etc. -- class simplified for question } public class ResourceView { public int Id { get; set; } public Resource Resource { get; set; } public User User { get; set; } public DateTime ViewDate { get; set; } }
Using NHibernate, how can I get the 5 most popular resources in order in a row, similar to what the following sql gets:
select * from [resource]
where id in (
select top 5 resourceId from resourceViews
where userId = 3
group by (resourceId)
Sort by number (*) Descending)
Bonus points if this can be done using the API criteria instead of hql.
source share