0
我正在使用NHibernate的Web應用程序。我有這樣一個模型:子查詢獲取屬性NHibernate
public class ProductViewModel {
public virtual int Id { get; set; }
/* others simple properties */
public virtual IList<OfferViewModel> LastOffers { get; set; }
public ProductViewModel() { }
}
public class OfferViewModel {
public virtual string UserName { get; set; }
public virtual decimal Prince { get; set; }
public OfferViewModel() { }
}
我想知道,如何將「LastOffers」集合屬性與HQL子查詢取?我想用前10個最後的優惠獲取它。我有我的模型映射正確,但我不知道sintax做一個子查詢獲取此屬性。
今天,我用這樣的命令來獲取我的ViewModel:
public IList<ProductViewModel> GetProductsForSalles()
{
return
Session.CreateQuery(@"select p.Id as Id,
p.Name as Name,
p.Price as Price,
p.Price as Date
/* FETCH LastOffers? */
from Product p
where p.Active=true and (p.Status=:Status)
order by a.Date asc")
.SetParameter("Status", Status.Started)
.SetMaxResults(50)
.SetResultTransformer(Transformers.AliasToBean<ProductViewModel>())
.List<ProductViewModel>();
}
謝謝!
你好天才,感謝awser。實際上,DistinctRootEntityResult可以解決我的疑難問題:),但是有什麼方法可以獲取前10條記錄的屬性並通過id desc命令?我希望這可以提高性能,因爲每個產品都會有很多優惠。再次感謝! – 2010-12-06 11:38:00