开发者

NHibernate Criteria query to select the most recent item per type

开发者 https://www.devze.com 2023-03-08 02:43 出处:网络
I need to find the 开发者_如何学运维most recent report submitted by members of staff, using an NHibernate criteria query. I\'m sure that I need to use projections, but I can\'t work out how to set it

I need to find the 开发者_如何学运维most recent report submitted by members of staff, using an NHibernate criteria query. I'm sure that I need to use projections, but I can't work out how to set it up.

A paraphrase of my domain model:

public class Employee
{
  public int Id {get; set;}
  public string Name {get; set;}
}

public class Report 
{
  public int Id {get; set;}
  public DateTime? Submitted {get; set;}
  public Employee Employee {get; set;}
  // Other report properties omitted
}

If there were 5 members of staff, who each have 7 reports, the query should return 5 reports, one per employee, with the Submitted property being not null, and being most recent one for that employee.


I am not quite sure about this, but check it out:

var rst = session.CreateCriteria<Report>()
                .SetProjection(Projections.GroupProperty("Employee"))
                .SetProjection(Projections.Max("Submitted"))
                .Add(NHibernate.Criterion.Expression.IsNotNull("Submitted")).List();
0

精彩评论

暂无评论...
验证码 换一张
取 消