Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just to add some of my own thoughts here. Because I am such a perfectionist I do not want to pull the entire contents of the table into memory just so I can do the calculation then sort then page. So somehow the table needs to know what the total is.</p> <p>What I was thinking was to create a new field in the line table called 'CalcTotal' that contains the calculated total of the line. This value would be set every time the line is changed, based on the value of .Total</p> <p>This has 2 advantages. Firstly, I can change the query to this:</p> <pre><code>var invoices = ( from c in _repository.Customers where c.Id == id from i in c.Invoices select new InvoiceIndex { Id = i.Id, CustomerName = i.Customer.Name, Attention = i.Attention, Total = i.Lines.Sum( l =&gt; l.CalcTotal ), Posted = i.Created, Salesman = i.Salesman.Name } ) </code></pre> <p>Sorting and paging will work because its a db field. And I can do a check ( line.CalcTotal == line.Total ) in my controller to detect any tom foolery. It does cause a little bit of overhead in my repository, namely when I go to save or create a new line I would have to add </p> <pre><code>line.CalcTotal = line.Total </code></pre> <p>but I think it may be worth it.</p> <p>Why go to the trouble of having 2 properties that have the same data?</p> <p>Well its true, I could put </p> <pre><code>line.CalcTotal = line.Quantity * line.Price; </code></pre> <p>in my repository. This would not be very orthogonal though, and if some change to line totals needed to happen, it would make much more sense to edit the partial Line class than the Line repository.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload