nHibernate allows you to sort column names easily, so long as the column name matches the mapping. So, for example:
Map(Function(x) x.CreatedOn)
criteria = criteria.AddOrder(New Global.NHibernate.Criterion.Order("CreatedOn", True))
Is all above board and cosy.
However I'm having trouble getting 开发者_运维问答the right syntax to order by properties which are inferred from the object rather than directly mapped. I'm not even sure if the latter is possible. Consider the following:
HasMany(Function(x) x.Shipments).Cascade.SaveUpdate()
Public Overridable ReadOnly Property Items() As IList(Of OrderItem)
Get
Return Shipments.SelectMany(Function(x) x.Items).ToList().AsReadOnly()
End Get
End Property
How would one order by Items.Count in that situation? Trying the objvious:
criteria = criteria.AddOrder(New Global.NHibernate.Criterion.Order("Items.Count", True))
Leads to an error of "could not resolve property: Items"
Cheers, Matt
To order by part of a component, you would specify the property name and then the component part, just ask if you were accessing it off the object in code.
criteria = criteria.AddOrder(New Global.NHibernate.Criterion.Order("Name.Surname", True))
As for your second question, this is not easily accomplished with Criteria, but can easily be written using HQL:
query = session.CreateQuery("select s from Shipment s order by size(s.Items)")
精彩评论