title: Kendo MVC ToDataSourceResult very slow with IQueryable EF function
body: <p>For some reason when I use kendo extension ToDataSourceResult, my EF imported function runs many times slower than directly in the SQL Server.</p>
  
  <p>My API:</p>
  
  <pre><code>public HttpResponseMessage Get([ModelBinder(typeof(Kendo.Mvc.UI.WebAPI.DataSourceRequestModelBinder))] DataSourceRequest dataSourceRequest)
  {
      DataSourceResult result = orderAccess.Get(UserID).ToDataSourceResult(dataSourceRequest);
  }
  </code></pre>
  
  <p>My Get function:</p>
  
  <pre><code>public IQueryable Get(Guid userID)
  {
      return db.fnOrdersGet(userID).AsQueryable();
  }
  </code></pre>
  
  <p>fnOrdersGet was imported into EF:</p>
  
  <pre><code>[DbFunction("EntityModel", "fnOrdersGet")]
  public virtual IQueryable&lt;fnOrdersResult&gt; fnOrdersGet(Nullable&lt;System.Guid&gt; userID) 
  {
      var userIDParameter = userID.HasValue ?
              new ObjectParameter("UserID", userID) :
              new ObjectParameter("UserID", typeof(System.Guid));
      return ((IObjectContextAdapter)this).ObjectContext.CreateQuery&lt;fnOrdersResult&gt;("[EntityModel].[fnOrdersGet](@UserID)", userIDParameter);
  }
  </code></pre>
  
  <p>Using Glimpse, the query generated for count was (it takes 10s to run):</p>
  
  <pre><code>SELECT 
  [GroupBy1].[A1] AS [C1]
  FROM ( SELECT 
      COUNT(1) AS [A1]
      FROM [dbo].[fnOrdersGet](@UserID) AS [Extent1]
  )  AS [GroupBy1]
  </code></pre>
  
  <p>And the paginated query was (it also takes 10s to run):</p>
  
  <pre><code>SELECT 
  [Project1].[C1] AS [C1], 
  [Project1].[OrderID] AS [OrderID] 
  FROM ( SELECT 
      [Extent1].[OrderID] AS [OrderID], 
      1 AS [C1]
      FROM [dbo].[fnOrdersGet](@UserID) AS [Extent1]
  )  AS [Project1]
  ORDER BY [Project1].[OrderID] DESC
  OFFSET 0 ROWS FETCH NEXT 20 ROWS ONLY
  </code></pre>
  
  <p>If I copy the queries shown above into SQL Server, they both run in a few milliseconds. </p>
  
  <p>What could be done to improve this?</p>