Bad OData Performance with Entity Framework and Top and OrderBy
<p>I am using OData 5.8.0 and EntityFramework 6.1.3, the query:</p>

<pre><code>&amp;$filter=fieldA eq 'ABCDEFG'&amp;$skip=0&amp;$top=10&amp;$orderby=fieldB desc
</code></pre>

<p>results in: </p>

<pre><code>SELECT TOP (10) 
    [Project1].[FieldA] AS [FieldA], 
    [Project1].[FieldB] AS [FieldB], 
    FROM ( SELECT [Project1].[FieldA] AS [FieldA], [Project1].[FieldB] AS [FieldB], row_number() OVER (ORDER BY [Project1].[FieldB] DESC, [Project1].[FieldA] ASC) AS [row_number]
        FROM ( SELECT 
            [Extent1].[FieldA] AS [FieldA], 
            [Extent1].[FieldB] AS [FieldB], 
            FROM [dbo].[table] AS [Extent1]
            WHERE ([Extent1].[FieldA] = 'ABCDEFG') OR (([Extent1].[FieldA] IS NULL) AND ('ABCDEFG' IS NULL))
        )  AS [Project1]
    )  AS [Project1]
    WHERE [Project1].[row_number] &gt; 0
    ORDER BY [Project1].[FieldB] DESC, [Project1].[FieldA] ASC
</code></pre>

<p>Which takes ~20 seconds to run against the DB for a large quantity of field A. </p>

<p>If I use the identical LINQ:</p>

<pre><code>var newList = table.Where(f =&gt; f.fieldA == 'ABCDEFG').OrderByDescending(f =&gt; f.fieldB).Take(10).Skip(0).ToList();
</code></pre>

<p>It results in:</p>

<pre><code>SELECT 
    [Limit1].[FieldA] AS [FieldA], 
    [Limit1].[FieldB] AS [FieldB]
    FROM ( SELECT [Limit1].[FieldA] AS [FieldA], [Limit1].[FieldB] AS [FieldB], row_number() OVER (ORDER BY [Limit1].[FieldB] DESC) AS [row_number]
        FROM ( SELECT TOP (10) [Project1].[FieldA] AS [FieldA], [Project1].[FieldB] AS [FieldB]
            FROM ( SELECT 
                [Extent1].[FieldA] AS [FieldA], 
                [Extent1].[FieldB] AS [FieldB]
                FROM [dbo].[table] AS [Extent1]
                WHERE ([Extent1].[FieldA] = 'ABCDEFG') OR (([Extent1].[FieldA] IS NULL) AND ('ABCDEFG' IS NULL))
            )  AS [Project1]
            ORDER BY [Project1].[FieldB] DESC
        )  AS [Limit1]
    )  AS [Limit1]
    WHERE [Limit1].[row_number] &gt; 0
    ORDER BY [Limit1].[FieldB] DES
</code></pre>

<p>Which takes 120ms to run. </p>

<p>How do I force OData to use the same expression (i.e. not to use TOP in the outer statement)? </p>