Currently in my MVC grids I am using normal Server Bindings, and the filters then are appended to the URL as query strings. The problem with this method is that if I am querying a grid that by default has thousands of records, but I am only displaying the first 30 records on the first page (paging filter) of my grid. Same thing would apply to a string filter for last name. I filter my 2000 records for last name smith, get 100 records, only 30 displayed on first page. I will then actually query a person object, have the full 2k objects returned, filter it to 100, and display 30. This is horribly inefficient.
How does one pass the filter parameters into a LINQ query for example so the initial query only returns results shown on that page? Also is there some automated way to do this generically for any grid? Or would you have to write this logic for every single grid you have?
I know if ToGridModel
, which I use when exporting a grid to excel:
public ActionResult Export(int page, string orderBy, string filter, string groupBy)
{
//Get the data representing the current grid state - page, sort and filter
List<Building> buildings = _buildingService.GetList();
List<BuildingModel> buildingModels = new List<BuildingModel>();
buildings.ForEach(x => buildingModels.Add(_buildingService.ConvertToModel(x)));
GridModel model = buildingModels.AsQueryable().ToGridModel(1, buildingModels.Count, orderBy, groupBy, filter);
MemoryStream fileOutput = ExcelUtil.ExportGridModelToExcel<BuildingModel>(model);
return File(fileOutput.ToArray(), //The binary data of the XLS file
"application/vnd.ms-excel", //MIME type of Excel files
"BuildingsReport.xls"); //Suggested file name in the "Save as" dialog which will be displayed to the end user
}
but I guess then another problem is that the grid itself is made up of ViewModels, not the POCO object. So even then, when I export to excel. I have to requery the whole result set, and then filter it down.
Surely there is a better way to do this?
I prefer to use
You can use Custom Binding to do this.
simple example of it you can read here: Telerik Documentation
For more generic approach you can use method
CreateFilterExpression
of classFilterDescriptor
Update
Generic example: