Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update:</strong> please read clarification in the end of answer for details about closure handling.</p> <p>First things first. This code:</p> <pre><code> for (int i = 0; i &lt; driveList.Count; i++) { var q = from sd in db.ServerDrives where sd.DriveLetter == driveList[i].Name select sd; //... } </code></pre> <p>won't work irregardless of curly braces. The reason for that is closure of <code>i</code> (which is masked out by funny LINQ syntax). The easiest way to fix it is to store <code>i</code> value (or referenced value) in separate variable.</p> <pre><code> for (int i = 0; i &lt; driveList.Count; i++) { var j = i; var q = from sd in db.ServerDrives where sd.DriveLetter == driveList[j].Name select sd; //... } </code></pre> <p>This will work, but it looks loosy. Better:</p> <pre><code> for (int i = 0; i &lt; driveList.Count; i++) { var drive = driveList[i]; var q = from sd in db.ServerDrives where sd.DriveLetter == drive.Name select sd; ServerDrive existingServerDrives = q.First(); existingServerDrives.FreeSpace = drive.FreeSpace; //.. db.SubmitChanges(); } </code></pre> <p>But why would you need loop in first place? Why not <code>foreach</code>?</p> <pre><code> foreach (var drive in driveList) { var q = from sd in db.ServerDrives where sd.DriveLetter == drive.Name select sd; ServerDrive existingServerDrives = q.First(); existingServerDrives.FreeSpace = drive.FreeSpace; //.. db.SubmitChanges(); } </code></pre> <p>But this won't work too, be careful. Same trouble with closures. Solution is easy:</p> <pre><code> foreach (var d in driveList) { var drive = d; var q = from sd in db.ServerDrives where sd.DriveLetter == drive.Name select sd; ServerDrive existingServerDrives = q.First(); existingServerDrives.FreeSpace = drive.FreeSpace; //.. db.SubmitChanges(); } </code></pre> <p>But of course such things are better with projections:</p> <pre><code> foreach (var d in driveList.Select(d =&gt; new {freeSpace = d.FreeSpace, existingServerDrives = db.ServerDrives.First(sd =&gt; sd.DriveLetter == d.Name)})) { d.existingServerDrives.FreeSpace = d.freeSpace; //.. db.SubmitChanges(); } </code></pre> <p><strong>Clarification</strong>: In fact, I am not completely right about "will not work" part. It <em>will</em> if <code>driveList</code> is <code>IQueryable</code>, but it <em>won't</em> if it is <code>IEnumerable</code> (because the former uses expressions, and the latter - delegates). But because syntax is same, it is all too easy to make this error, so you still should not use loop variables inside lambda expressions irregardless of their real type.</p>
    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.
 

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