检索最近5个订单的单位
本文关键字:单位 最近 5个 检索 | 更新日期: 2023-09-27 18:15:45
参考:从不同表中选择的nhibernate条件
我试图检索例如最后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个订单的数量?
Thx
试试这个:
//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();
解决方案如下:
Model.Order orderAlias = null;
Model.Unit unitsAlias = null;
Model.Employee employeeAlias = null;
IList<Model.Unit> itemList = null;
using (m_hibernateSession.BeginTransaction())
{
IQueryOver<Model.Order, Model.Order> orderQuery = m_hibernateSession.QueryOver<Model.Order>();
// order related filters
if (propertiesNames.Keys.Contains("OrderPONumber")) orderQuery.Where(o => o.PONumber == Int32.Parse((string)propertiesNames["OrderPONumber"]));
// retrieve amount of orders (e.g. 5)
orderQuery.Where(o => o.PONumber != 0).Select(o => o.ID).OrderBy(o => o.PONumber).Desc.Take(amount);
int[] orderIDList = orderQuery.List<int>().ToArray(); // get ID list of filtered orders
IQueryOver<Model.Unit, Model.Unit> query = m_hibernateSession.QueryOver<Model.Unit>(() => unitsAlias)
.WhereRestrictionOn(o => o.OrderRef.ID).IsIn(orderIDList) // set order ID range
.JoinAlias(() => unitsAlias.EmployeeRef, () => employeeAlias, JoinType.InnerJoin);
// additional filters
if (propertiesNames.Keys.Contains("CostCenter")) query.Where(() => unitsAlias.CostCenter == (string)propertiesNames["CostCenter"]);
if (propertiesNames.Keys.Contains("Employee")) query.Where(Restrictions.On(() => employeeAlias.Name).IsLike( "%" + (string)propertiesNames["Employee"] + "%"));
itemList = query.List<Model.Unit>();
}