private static IQueryable <T> Filter <T>(IQueryable <T> queryable, CustomFilter filter) { if (filter != null && filter.Logic != null) { // Collect a flat list of all filters var filters = filter.All(); // Get all filter values as array (needed by the Where method of Dynamic Linq) var values = filters.Select(f => f.Value).ToArray(); // Create a predicate expression e.g. Field1 = @0 And Field2 > @1 string predicate = filter.ToExpression(filters); // Use the Where method of Dynamic Linq to filter the data queryable = queryable.Where(predicate, values); } return(queryable); }
public ActionResult Read(int take, int skip, IEnumerable<Sort> sort, CustomFilter filter) { using (var northwind = new NorthwindEntities()) { var result = northwind.Products .OrderBy(p => p.ProductID) // EF requires ordered IQueryable in order to do paging // Use a view model to avoid serializing internal Entity Framework properties as JSON .Select(p => new ProductViewModel { ProductID = p.ProductID, ProductName = p.ProductName, UnitPrice = p.UnitPrice, UnitsInStock = p.UnitsInStock, Discontinued = p.Discontinued }) .ToDataSourceResult(take, skip, sort, filter); return Json(result); } }
/// <summary> /// Applies data processing (paging, sorting and filtering) over IQueryable using Dynamic Linq. /// </summary> /// <typeparam name="T">The type of the IQueryable</typeparam> /// <param name="queryable">The IQueryable which should be processed.</param> /// <param name="take">Specifies how many items to take. Configurable via the pageSize setting of the Kendo DataSource.</param> /// <param name="skip">Specifies how many items to skip.</param> /// <param name="sort">Specifies the current sort order.</param> /// <param name="filter">Specifies the current filter.</param> /// <returns>A DataSourceResult object populated from the processed IQueryable.</returns> public static DataSourceResult ToDataSourceResult <T>(this IQueryable <T> queryable, int take, int skip, IEnumerable <Sort> sort, CustomFilter filter) { // Filter the data first queryable = Filter(queryable, filter); // Calculate the total number of records (needed for paging) var total = queryable.Count(); // Sort the data queryable = Sort(queryable, sort); // Finally page the data queryable = Page(queryable, take, skip); return(new DataSourceResult { Data = queryable.ToList(), Total = total }); }