Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the units of the last 5 orders

Reference: nhibernate criteria for selecting from different tables

I tried to retrieve e.g. last 5 orders but I got the last 5 units!

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>();
}

Which changes are needed to get the units of the last 5 orders?

Thx

like image 865
leon22 Avatar asked Nov 12 '22 04:11

leon22


1 Answers

Try this:

    //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();
like image 180
Ahmed KRAIEM Avatar answered Nov 15 '22 11:11

Ahmed KRAIEM