5

参考:从不同表中选择的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 个订单的单位,需要进行哪些更改?

谢谢

4

2 回答 2

1

尝试这个:

    //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();
于 2013-05-27T13:27:05.377 回答
0

这是解决方案:

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>();
}
于 2013-06-04T11:49:16.437 回答