Note that there are some explanatory texts on larger screens.

plurals
  1. POlinq 'range variable' problem
    text
    copied!<p>I have a strange problem when deleteting records using linq, my suspicion is that it has something to do with the range variable (named <code>source</code>). After deleting a record all targets for a customer are retrieved using the following statement:</p> <pre><code>var q = from source in unitOfWork.GetRepository&lt;db_Target&gt;().Find() where source.db_TargetBase.db_Person.fk_Customer == customerID select source.FromLinq(); </code></pre> <p>where FromLinq is in extention method on db_target:</p> <pre><code>public static Target FromLinq(this db_Target source) { return new Target { id = source.id, LastModified = source.db_TargetBase.LastModified, ... } } </code></pre> <p>When a record is deleted both <code>db_Target</code> and <code>db_TargetBase</code> are deleted. When, for example, two users are deleting records, linq tries to retrieve a record for user2 which is deleted by user1, causing a crash on the <code>LastModified = source.db_TargetBase.LastModified</code> line because <code>db_TargetBase</code> is <code>null</code>.</p> <p>When using the following code the problem does not occure and only the non-deleted records are retrieved:</p> <pre><code>var q = from source in unitOfWork.GetRepository&lt;db_Target&gt;().Find() where source.db_TargetBase.db_Person.fk_Customer == customerID select new Target { id = source.id, LastModified = source.db_TargetBase.LastModified, ... }; </code></pre> <p>This spawns two questions:</p> <ol> <li>What is happening here? Am I making a copy of the range variable <code>source</code> because I'm using it in a extention method?</li> <li>How can I "wrap" the <code>return new Target</code> code? I am using this in multiple places and do not want to copy it every time. Making my code harder to maintain.</li> </ol> <p>TIA,</p> <p>JJ</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