2

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
    {
        decimal 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?

olle
  • 4,597
  • 24
  • 28

1 Answers1

2

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

Apostolis Bekiaris
  • 2,145
  • 2
  • 18
  • 21