简体   繁体   English

NHibernate 条件查询到 select 每个类型的最新项目

[英]NHibernate Criteria query to select the most recent item per type

I need to find the most recent report submitted by members of staff, using an NHibernate criteria query.我需要使用 NHibernate 条件查询查找工作人员提交的最新报告。 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:我的域 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 .如果有 5 名员工,每人有 7 份报告,则查询应返回 5 份报告,每位员工一份,提交的属性不是 null,并且是该员工最近的一份。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM