Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>LINQ isn't all that useful for executing side effects, it's primarily intended for <em>querying</em>. In truth, the fact that it has deferred execution so engrained in its behaviour makes it a <em>poor choice</em> for executing side-effects, IMO.</p> <p>The code you've got looks perfectly fine to me. If you did want to use LINQ though, I doubt you could improve much on:</p> <pre><code>foreach (var o in obj.Where(i =&gt; i.SomeProperty == Something)) { o.SomeOtherProperty = true; } </code></pre> <p>Now, that isn't all that better (arguably worse) than your original.</p> <p>On the other hand, if you wanted to create a <em>new,</em> streaming sequence with <em>projections</em> of the original items having the desired characteristics, you could do something like:</p> <pre><code>var projection = obj.Where(i =&gt; i.SomeProperty == Something) .Select(i =&gt; new Foo(i) { SomeOtherProperty = true }); // assuming your type has a copy-constructor </code></pre> <p><strong>EDIT</strong>: If you don't want to heed the advice of the experts (read: Eric Lippert), you can write your own extension-method:</p> <pre><code>public static void Do&lt;T&gt;(this IEnumerable&lt;T&gt; source, Action&lt;T&gt; action) { if (source == null) throw new ArgumentNullException("source"); if (action == null) throw new ArgumentNullException("action"); foreach (T item in source) action(item); } </code></pre> <p>This will allow you to do:</p> <pre><code>obj.Where(o =&gt; o.SomeProperty == Something) .Do(o =&gt; o.SomeOtherProperty = true); </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.
    3. 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