参考:nhibernate criteria for selecting from different tables
我试着取回最后5个订单,但我拿到了最后5个!
Model.Order orderAlias = null;
Model.Unit unitsAlias = null;
Model.Employee employeeAlias = null;
IList<Model.Unit> itemList = null;
using (m_hibernateSession.BeginTransaction())
{
var query = m_hibernateSession.QueryOver<Model.Unit>(() => unitsAlias)
.JoinAlias(() => unitsAlias.OrderRef, () => orderAlias, JoinType.InnerJoin)
.JoinAlias(() => unitsAlias.EmployeeRef, () => employeeAlias, JoinType.InnerJoin);
// add additional filters if set
if (propertiesNames.Keys.Contains("Employee")) query.Where(Restrictions.On(() => employeeAlias.Name).IsLike( "%" + (string)propertiesNames["Employee"] + "%"));
if (propertiesNames.Keys.Contains("Division")) query.Where(() => unitsAlias.Division == (string)propertiesNames["Division"]);
query.Where(() => orderAlias.PONumber != 0).OrderBy(() => orderAlias.PONumber).Desc.Take(5);
itemList = query.List<Model.Unit>();
}
最后5个订单的单位需要做哪些更改?
谢谢
最佳答案
试试这个:
//A List (with a maximum of 5 elements) of Lists of Units
List<List<Model.Unit>> listOfUnits = m_hibernateSession
.Query<Order>()
.OrderByDescending(o=>o.PONumber)
.Where(o => o.PONumber != 0)
.Take(5)
.Select(o => o.Units.ToList())
.ToList();
//OR, a List of all Units for the last 5 Orders
List<Model.Unit> listOfUnits = m_hibernateSession
.Query<Order>()
.OrderByDescending(o=>o.PONumber)
.Where(o => o.PONumber != 0)
.Take(5)
.SelectMany(o => o.Units)
.ToList();