FIX: RadGrid returns filter expression with SQL syntax instead LINQ when is bind to an IQueryable.
Posted on:22 Jun 2015 11:57
When the grid is bound to query generated by the open access context and the query is executed the result is saved in an object of type Telerik.OpenAccess.Query.Piece(Of T). Then this object is cast by the framework to IQueryable and the result is saved in object of type Telerik.OpenAccess.Query.ExtentQueryImpl(Of T) which is a forward only. The RadGrid does not recognize this type as IQueryable and it generates the default sql filter expression. A simple workaround here is to call ToList() method on the result query:
grid.DataSource = dataSource.Where(grid.MasterTableView.FilterExpression).Skip(startRowIndex).Take(maximumRows).ToList()
This will generate following sql query in the database which is optimized and will get only the filtered data for the grid’s current page:
declare @p1 int
exec sp_prepexec @p1 output,N'@__TAKE int',N'SELECT TOP(@__TAKE) a.[OrderID] AS COL1, a.[CustomerID] AS COL2, a.[CustomerID] AS COL3, a.[EmployeeID] AS COL4, a.[EmployeeID] AS COL5, a.[Freight] AS COL6, a.[OrderDate] AS COL7, a.[RequiredDate] AS COL8, a.[ShipAddress] AS COL9, a.[ShipCity] AS COL10, a.[ShipCountry] AS COL11, a.[ShipName] AS COL12, a.[ShipPostalCode] AS COL13, a.[ShipRegion] AS COL14, a.[ShipVia] AS COL15, a.[ShippedDate] AS COL16, a.[ShipVia] AS COL17 FROM [Orders] a WHERE (CASE WHEN a.[ShipCountry] IS NULL THEN '''' ELSE a.[ShipCountry] END) LIKE ''%S%'' ESCAPE ''\'' ORDER BY COL8 DESC ',@__TAKE=10