Replacement for for... if array iteration
<p>I love list comprehensions in Python, because they concisely represent a transformation of a list.</p>

<p>However, in other languages, I frequently find myself writing something along the lines of:</p>

<pre><code>foreach (int x in intArray)
  if (x &gt; 3) //generic condition on x
    x++ 
    //do other processing
</code></pre>

<p>This example is in C#, where I'm under the impression LINQ can help with this, but is there some common programming construct which can replace this slightly less-than-elegant solution?  Perhaps a data structure I'm not considering?</p>

<p>The increment in the original <code>foreach</code> loop will not affect the contents of the array, the only way to do this remains a <code>for</code> loop:</p>

<pre><code>for(int i = 0; i &lt; intArray.Length; ++i)
{
    if(intArray[i] &gt; 3) ++intArray[i];
}
</code></pre>

<p>Linq is not intended to modify existing collections or sequences. It creates new sequences based on existing ones. It is possible to achieve the above code using Linq, though it is slightly against its purposes:</p>

<pre><code>var newArray1 = from i in intArray select ((i &gt; 3) ? (i + 1) : (i));
var newArray2 = intArray.Select(i =&gt; (i &gt; 3) ? (i + 1) : (i));
</code></pre>

<p>Using <code>where</code> (or equivalent), as shown in some of the other answers, will exclude any values less than or equal to 3 from the resulting sequence.</p>

<pre><code>var intArray = new int[] { 10, 1, 20, 2 };
var newArray = from i in intArray where i &gt; 3 select i + 1;
// newArray == { 11, 21 }
</code></pre>

<p>There is a <code>ForEach</code> method on arrays that will allow you to use a lambda function instead of a <code>foreach</code> block, though for anything more than a method call I would stick with <code>foreach</code>.</p>

<pre><code>intArray.ForEach(i =&gt; DoSomething(i));
</code></pre>
