/// <summary> /// Pretreatment of specific datetime condition and disallowed value type /// </summary> /// <param name="filter"></param> /// <returns></returns> private static Filter PreliminaryWork(Filter filter) { if (filter.Filters != null && filter.Logic != null) { var newFilters = new List <Filter>(); foreach (var f in filter.Filters) { newFilters.Add(PreliminaryWork(f)); } filter.Filters = newFilters; } // Used when the datetime's operator value is eq and local time is 00:00:00 if (filter.Value is DateTime utcTime && filter.Operator == "eq") { // Copy the time from the filter var localTime = utcTime.ToLocalTime(); if (localTime.Hour != 0 || localTime.Minute != 0 || localTime.Second != 0) { return(filter); } var newFilter = new Filter { Logic = "and" }; var filtersList = new List <Filter> { // Instead of comparing for exact equality, we compare as greater than the start of the day... new Filter { Field = filter.Field, Filters = filter.Filters, Value = new DateTime(localTime.Year, localTime.Month, localTime.Day, 0, 0, 0), Operator = "gte" }, // ...and less than the end of that same day (we're making an additional filter here) new Filter { Field = filter.Field, Filters = filter.Filters, Value = new DateTime(localTime.Year, localTime.Month, localTime.Day, 23, 59, 59), Operator = "lte" } }; newFilter.Filters = filtersList; return(newFilter); } // Convert datetime to local if (filter.Value is DateTime utcTime2) { var localTime = utcTime2.ToLocalTime(); filter.Value = new DateTime(localTime.Year, localTime.Month, localTime.Day, localTime.Hour, localTime.Minute, localTime.Second, localTime.Millisecond); } // When we have a decimal value it gets converted to double and the query will break if (filter.Value is Double) { filter.Value = Convert.ToDecimal(filter.Value); } return(filter); }
/// <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> /// <param name="group">Specifies the current groups.</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, Filter filter, IEnumerable <Aggregator> aggregates, IEnumerable <Group> group) { var errors = new List <object>(); // Filter the data first queryable = Filter(queryable, filter, errors); // Calculate the total number of records (needed for paging) var total = queryable.Count(); // Calculate the aggregates var aggregate = Aggregates(queryable, aggregates); if (group != null && group.Any()) { //if(sort == null) sort = GetDefaultSort(queryable.ElementType, sort); if (sort == null) { sort = new List <Sort>(); } foreach (var source in group.Reverse()) { sort = sort.Append(new Sort { Field = source.Field, Dir = source.Dir }); } } // Sort the data queryable = Sort(queryable, sort); // Finally page the data if (take > 0) { queryable = Page(queryable, take, skip); } var result = new DataSourceResult { Total = total, Aggregates = aggregate }; // Group By if ((group != null) && group.Any()) { //result.Groups = queryable.ToList().GroupByMany(group); result.Groups = queryable.GroupByMany(group); } else { result.Data = queryable.ToList(); } // Set errors if any if (errors.Any()) { result.Errors = errors; } return(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, Filter filter) { return(queryable.ToDataSourceResult(take, skip, sort, filter, null, null)); }