初学者问题,我确定:
我正在尝试在2个表之间进行联接,并同时限制结果,如下所示:
var bookings = session.CreateCriteria<Booking>("p")
.CreateCriteria("p.BookingLocations", JoinType.InnerJoin)
.Add(Restrictions.Eq("p.BookingLocations.locationID", locationId))
.SetMaxResults(30)
.List<Booking>();
我得到错误:
无法解析媒体资源:BookingLocations.locationID:预订
我可以看到Booking.BookingLocation包含许多记录,而Booking和BookingLocation之间存在一对多关系,但是我不确定这是否是问题的原因。
我想如果是的话,我需要做些类似的事情:
.Add(Restrictions.Eq("p.BookingLocations.first().locationID", locationId))
...但毫无疑问,那是行不通的;)
班级
public class Booking
{
public virtual int Id { get; set; }
public virtual Int32 bookingID { get; set; }
public virtual Int32 bookingAdminID { get; set;
}
public class BookingLocation
{
public virtual int Id { get; set; }
public virtual Int32 bookingID { get; set; }
public virtual Int32 locationID { get; set; }
}
对应
public BookingMap()
{
Table("Bookings");
Id(x => x.Id).Column("ID");
Map(x => x.bookingID).Column("BookingID");
Map(x => x.bookingAdminID).Column("BookingAdminID");
}
public class BookingLocation
{
public virtual int Id { get; set; }
public virtual Int32 bookingID { get; set; }
public virtual Int32 locationID { get; set; }
}
最佳答案
子条件定义了一个新范围,该范围植根于BookingLocation。您只需要使用locationID
:
session.CreateCriteria<Booking>("p")
.CreateCriteria("p.BookingLocations", JoinType.InnerJoin)
.Add(Restrictions.Eq("locationID", locationId))