[英]NHibernate Criteria query to select the most recent item per type
我需要使用 NHibernate 條件查詢查找工作人員提交的最新報告。 我確定我需要使用投影,但我不知道如何設置它。
我的域 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
}
如果有 5 名員工,每人有 7 份報告,則查詢應返回 5 份報告,每位員工一份,提交的屬性不是 null,並且是該員工最近的一份。
我不太確定這一點,但請查看:
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.