Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework with OData(Web API) is sending Order By clause By default to Sql Query

I am using Web Api with OData. and I have an entity defined in an EF 5.0.
I am sending very simple request to Controller::

 $.ajax({url: "/odata/Details?$top=10",
            type: "GET",
            dataType: 'json',
            success: function (data) {
               viewModel.list(data.value);
            }

Now code on My controller::

 [Queryable]
    public override IQueryable<Area> Get()
    {  
    return db.Area.AsQueryable();
    }

Query i see using SQL Profiler::

 SELECT TOP (@p__linq__1) 
[Project1].[id] AS [id1], 
[Project1].[name] AS [name1], 
[Project1].[pucrhase] AS [pucrhase1], 
[Project1].[sale] AS [sale1]
FROM Area
ORDER BY [Project1].[id] DESC, [Project1].[name] ASC, [Project1].[pucrhase] ASC,      
[Project1].[sale] ASC,N',@p__linq__1 int,@p__linq__1=10

I have not requested for any Ordering , Order By Clause . EF adds ORDER BY clause by Itself to Query. Order By clause added contains all columns of table.This table has 3 millions records and Query is timing Out as it is Ordering by All columns .

I tested by removing Order By it took Less then a second to finish

So Question is

how to Stop Entity framework(support of Web Api Odata ) from sending Order By clause to Sql Query.

How to remove Order By clause from SQL Query Entity framework(Web Api Odata) runs on Server?

Any Help is appreciated.

like image 374
Rahul Kamboj Avatar asked Apr 17 '13 05:04

Rahul Kamboj


1 Answers

Im not sure if this is the right answer, but im assuming that the odata service is attempting to maintain a stable sort ordering by ordering on all properties within your model.

Therefore try

[Queryable(EnsureStableOrdering=false)]
like image 173
Alistair Avatar answered Oct 03 '22 03:10

Alistair