Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know about "best possible", but here's how to do it in linq to sql.</p> <pre><code>using (CustomDataContext dc = new CustomDataContext()) { List&lt;Row&gt; rows = dc.Rows .Where(row =&gt; startingID &lt;= row.ID) .OrderBy(row =&gt; row.ID) .ToList(); Row firstRow = rows.First(); int firstRowAmount = firstRow.Amount; dc.Rows.DeleteOnSubmit(firstRow); foreach(Row row in rows.Skip(1)) { row.Amount -= firstRowAmount; } dc.SubmitChanges(); } </code></pre> <p>The advantages here are:</p> <ul> <li>Single transaction - the whole update goes or none of it.</li> <li>Optimistic concurrency - if someone removes or updates one of our rows between our read and our write, our changes are rejected.</li> <li>Clear maintainable c# code.</li> </ul> <p>The downsides are:</p> <ul> <li>The records must be read into memory before they can be updated.</li> <li>One update statement is generated per row.</li> </ul> <hr> <p>Since you have a million rows to update, the above is kind of slow and wasteful... You're going to have to update those rows in fewer sql commands and without reading the rows into memory.</p> <pre><code>using(TransactionScope ts = new TransactionScope()) { using (CustomDataContext dc = new CustomDataContext()) { Row theRow = dc.Rows.Single(row =&gt; startingID == row.ID) int firstRowAmount = theRow.Amount; dc.Rows.DeleteOnSubmit(theRow); dc.SubmitChanges(); dc.ExecuteCommand(@"Update Row SET Amount = Amount - {0} WHERE {1} &lt; ID", firstRowAmount, startingID); ts.Complete(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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