Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I need to perform a deep clone to get this code to work?
    primarykey
    data
    text
    <p>The following code works. However, it works because I end up creating a deep clone of the suppliers. If I do not perform a deep clone then we get an error suggesting that the supplier objects have changed and the attempt to amend the supplier table has failed. This only happens if the following line is run: <code>foreach (Supplier suppliers in exceptions)</code>. Oddly, this occurs irrespective whether the <code>Delete()</code> method is executed. Why does this happen? I have posted the working code below for your inspection. As I say, if you try looping without deep cloning then it does not work... Any ideas? </p> <pre><code>public void DeleteSuppliers(IList&lt;Supplier&gt; suppliers, Int32 parentID) { // If a supplier has been deleted on the form we need to delete from the database. // Get the suppliers from the database List&lt;Supplier&gt; dbSuppliers = Supplier.FindAllByParentID(parentID); // So return any suppliers that are in the database that are not now on this form IEnumerable&lt;Supplier&gt; results = dbSuppliers.Where(f =&gt; !Suppliers.Any(d =&gt; d.Id == f.Id)); IList&lt;Supplier&gt; exceptions = null; // code guard if (results != null) { // cast as a list IList&lt;Supplier&gt; tempList = (IList&lt;Supplier&gt;)results.ToList(); // deep clone otherwise there would be an error exceptions = (IList&lt;Supplier&gt;)ObjectHelper.DeepClone(tempList); // explicit clean up tempList = null; } // Delete the exceptions from the database if (exceptions != null) { // walk the suppliers that were deleted from the form foreach (Supplier suppliers in exceptions) { // delete the supplier from the database suppliers.Delete(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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