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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With