我有一个供应商。每个供应商都有几个 Reservations,上面有一个 ReservationDate。
我想要一份今天尚未预订的供应商列表。
在 SQL 中,我会做这样的事情:
SELECT v.Id, MAX(r.ReservationDate) AS MaxDate FROM Vendor v
INNER JOIN DailyReservation r ON v.Id = r.Vendor_Id
GROUP BY v.Id
HAVING MAX(r.ReservationDate) <> '2010-06-04'
我正在尝试在 NHibernate 中这样做:
session.CreateCriteria<Vendor>()
.CreateAlias("Reservations", "r")
.SetProjection(Projections.Alias(Projections.Max("r.ReservationDate"), "MaxDate"))
.Add(Restrictions.Not(Restrictions.Eq("MaxDate", DateTime.Today)))
.List<Vendor>();
这显然行不通。我究竟做错了什么?
编辑!我又玩了一些,到了这一点,效果更好:
var c = Session.CreateCriteria<Vendor>();
c.CreateAlias("Reservations", "r");
ProjectionList projections = Projections.ProjectionList();
projections.Add(Projections.Max("r.ReservationDate"), "MaxDate");
projections.Add(Projections.GroupProperty("Id"));
c.SetProjection(projections);
c.Add(Restrictions.Not(Restrictions.Eq("MaxDate", DateTime.Today)));
return c.List<Vendor>();
为了回答评论,我收到错误“NHibernate.QueryException: could not resolve property: MaxDate of: Vendor”