Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you're trying to do here isn't a particularly good thing to do with Linq as Linq is meant to be for querying data and not updating it. Your <code>Select</code> statement is relying on side-effects to perform the update. This generally should be avoided.</p> <p>However, you can still do what you want.</p> <p>To start with, I re-arranged your query as:</p> <pre><code>var items = obj1 .Where(w =&gt; w.Key == iKey) .Select(c =&gt; { c.Value = newValue; return c; }) .ToArray(); </code></pre> <p>Then I refactored it as such:</p> <pre><code>Func&lt;Obj, string, Obj&gt; update = (c, v) =&gt; { c.Value = v; return c; }; var items = (from w in obj1 where w.Key == iKey select update(w, newValue)).ToArray(); </code></pre> <p>This still has the side-effect, but I made it more explicit (and hopefully more readable).</p> <p>Given this refactoring, the <code>UPDATE</code> query involving the two lists becomes:</p> <pre><code>var UPDATE = (from o1 in obj1 join o2 in obj2 on o1.Key equals o2.Key select update(o1, o2.Value)).ToArray(); </code></pre> <p>If you wanted to do this without side-effects, I would suggest the following:</p> <pre><code>var items = from w in obj1 where w.Key == iKey select (Action)(() =&gt; w.Value = newValue); Array.ForEach(items.ToArray(), a =&gt; a()); var UPDATE = from o1 in obj1 join o2 in obj2 on o1.Key equals o2.Key select (Action)(() =&gt; o1.Value = o2.Value); Array.ForEach(UPDATE.ToArray(), a =&gt; a()); </code></pre> <p>You might not like this syntax, so you could easily write quick extension method on <code>IEnumerable&lt;Action&gt;</code> to invoke the actions and would make the code look like this:</p> <pre><code>(from w in obj1 where w.Key == iKey select (Action)(() =&gt; w.Value = newValue)).Invoke(); (from o1 in obj1 join o2 in obj2 on o1.Key equals o2.Key select (Action)(() =&gt; o1.Value = o2.Value)).Invoke(); </code></pre> <p>I hope this helps.</p>
    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.
    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