Type inference with control statements
<pre><code>var sortQ = filterQ;  // &lt;--- update

if (iSortingCols &gt; 0)
{
    sortQ = (sortDirs[0] == "asc") ?
        sortQ.OrderBy((d) =&gt; d.GetColumnData()[sortCols[0]]) :
        sortQ.OrderByDescending((d) =&gt; d.GetColumnData()[sortCols[0]]);

    if (iSortingCols &gt; 1)
    {
        for (int i = 1; i &lt; iSortingCols; i++)
        {
            sortQ = (sortDirs[i] == "asc") ?
                sortQ.ThenBy(d =&gt; d.GetColumnData()[sortCols[i]]) :
                sortQ.ThenByDescending(d =&gt; d.GetColumnData()[sortCols[i]]);

        }
     }
 }
</code></pre>

<p>The compiler underlines the two results of the ternary operator inside the for loop, saying that IEnumerable&lt;...> doesn't have a method overload called ThenBy (and similarly for ThenByDescending), but the sortQ will be an IOrderedEnumerable&lt;...> in that block.  Why isn't C# type inference picking that up?</p>

<p><strong>Update</strong>: for those who might have been confused before, what I left out of the original snippet was that I was assigning sortQ to the result of another query which forced the type inference engine to type sortQ as an IEnumerable&lt;..>, which of course is what was screwing up my example.  The answer below is the way around this.  Thanks to @Marc for reading between the lines.</p>
