How to express the query
Select * from
(select name, Max(date) as date from items group by name) as latest
inner join items as i
on i.name=latest.name and i.date = latest.date
in NHibernate's QueryOver syntax?
As a result I expect to get records with maximum dates for each name from items table.
Each item in table correspond to the ItemEnity class:
[Class(Table = "items")]
public class ItemEnity
{
[Id(Name = "Id")]
[Generator(1, Class = "native")]
public virtual long Id { get; set; }
[Property(NotNull = true)]
public virtual string Name { get; set; }
[Property(NotNull = true)]
public virtual DateTime Date { get; set; }
// other columns ...
}
I've managed to express subquery in a strongly typed form:
public class ItemLatest
{
public string Name { get; set; }
public DateTime Date { get; set; }
}
// ...
ItemLatest latest = null;
var subquery = QueryOver.Of<ItemEntity>().SelectList(list => list
.SelectGroup(i => i.Name).WithAlias(() => latest.Name)
.SelectMax(i => i.Date).WithAlias(() => latest.Date))
.TransformUsing(Transformers.AliasToBean<ItemLatest>());
var result = subquery.GetExecutableQueryOver(session).List<ItemLatest>();
But I have no clue how to write join because I can't find out how to use JoinQueryOver() to express connection between two entities (ItemLatest and ItemEnity in my case) that has no relation property referencing from one to another.
Query<T>()in a Linq syntax? It's under theNHibernate.Linqnamespace