devexpress XPO calculated property on child collection select n + 1
I have a XPO domain object Order which has a association with OrderLine. In order I have a property Profit which is implemented as
public decimal Profit
{
get
{
decima开发者_运维技巧l result = 0;
foreach (OrderLine ol in this.OrderLines)
{
result += ol.Profit.AsMoney() * ol.Quantity;
}
return result;
}
}
but this causes a listing of orders to execute a query per orderline for each order. How can I either load the collection of orderlines in a second query and join them in memory or if that is not feasible some way prevent the loading of the Profit property until it is actually accessed?
You can use the Session Prefetch method to accomplish your task
Session.PreFetch(ObjectList, "OrderLines");
Also consider looking at this sample from DevExpress Code Central E305. There the calculated property is being cached. In result the performance is increased more
精彩评论