How to get the latest object using Castle ActiveRecord
-
25-01-2021 - |
Question
I've got a Table of OrderDetails objects and I'd like to get OrderNumber of the latest Order in the database i.e. the Order with the highest OrderDetailsID. In SQL I can do the following:
select Top 1 OrderNumber from orderdetails order by OrderDetailsID desc
How would I go about getting the same thing using ActiveRecord, what Criteria should I be specifying in FindOne(...) call?
La solution
ActiveRecord.AsQueryable<OrderDetails>()
.OrderByDescending(o => o.OrderDetailsID).First().OrderNumber
I can't make it any shorter :) you could also do:
FindFirst(typeof (OrderDetails),
new[] {NHibernate.Criterion.Order.Desc("OrderDetailsID")}, null).OrderNumber;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow