private static IQueryable <T> Filter <T>(IQueryable <T> queryable, KendoFilter 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); }
/// <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 KendoDataSourceResult ToDataSourceResult <T>(this IQueryable <T> queryable, int take, int skip, IEnumerable <KendoSort> sort, KendoFilter filter) { return(queryable.ToDataSourceResult(take, skip, sort, filter, null)); }
/// <summary> /// Applies data processing (paging, sorting, filtering and aggregates) 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> /// <param name="aggregates">Specifies the current aggregates.</param> /// <returns>A DataSourceResult object populated from the processed IQueryable.</returns> public static KendoDataSourceResult ToDataSourceResult <T>(this IQueryable <T> queryable, int take, int skip, IEnumerable <KendoSort> sort, KendoFilter filter, IEnumerable <KendoAggregator> aggregates) { // Filter the data first queryable = Filter(queryable, filter); // Calculate the total number of records (needed for paging) var total = queryable.Count(); // Calculate the aggregates var aggregate = Aggregate(queryable, aggregates); // Sort the data queryable = Sort(queryable, sort); // Finally page the data if (take > 0) { queryable = Page(queryable, take, skip); } return(new KendoDataSourceResult { Data = queryable.ToList(), Total = total, Aggregates = aggregate }); }