From d6ba473a0b5efbba7408297e3ce4460bb71c6ede Mon Sep 17 00:00:00 2001 From: jandic Date: Tue, 22 Oct 2019 07:48:00 +0200 Subject: [PATCH 1/3] Update DataTablesFiltering.cs Fixed an issue when filtering on a data table throws an exception in cases there are no relevant columns to be filtered. Example: data table contains only numerical columns and a user types 'a letter'. Since the letter cannot be converted to a number, no where clause is generated and data.Where command was failing. --- Mvc.JQuery.DataTables.Common/DataTablesFiltering.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Mvc.JQuery.DataTables.Common/DataTablesFiltering.cs b/Mvc.JQuery.DataTables.Common/DataTablesFiltering.cs index 1a9cb4f..7efec6c 100644 --- a/Mvc.JQuery.DataTables.Common/DataTablesFiltering.cs +++ b/Mvc.JQuery.DataTables.Common/DataTablesFiltering.cs @@ -30,7 +30,11 @@ public IQueryable ApplyFiltersAndSort(DataTablesParam dtParameters, IQuery } } var values = parts.Where(p => p != null); - data = data.Where(string.Join(" or ", values), parameters.ToArray()); + var filterClause = string.Join(" or ", values); + if (string.IsNullOrWhiteSpace(filterClause) == false) + { + data = data.Where(filterClause, parameters.ToArray()); + } } for (int i = 0; i < dtParameters.sSearchValues.Count; i++) { From 313ced6903fc8c3e48dea77ad0f9afa4f03e2b7a Mon Sep 17 00:00:00 2001 From: jandic Date: Tue, 22 Oct 2019 11:17:51 +0200 Subject: [PATCH 2/3] Allowing for custom filter definition Allowing for custom filter definitions by a user and fixing a bug in RegisterFilter method. --- Mvc.JQuery.DataTables.Common/DataTablesFiltering.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Mvc.JQuery.DataTables.Common/DataTablesFiltering.cs b/Mvc.JQuery.DataTables.Common/DataTablesFiltering.cs index 7efec6c..0d2ce61 100644 --- a/Mvc.JQuery.DataTables.Common/DataTablesFiltering.cs +++ b/Mvc.JQuery.DataTables.Common/DataTablesFiltering.cs @@ -7,7 +7,7 @@ namespace Mvc.JQuery.DataTables { - internal class DataTablesFiltering + public class DataTablesFiltering { public IQueryable ApplyFiltersAndSort(DataTablesParam dtParameters, IQueryable data, DataTablesPropertyInfo[] columns) { @@ -118,7 +118,7 @@ private static ReturnedFilteredQueryForType Guard(Func(GuardedFilter filter) { - Filters.Add(Guard(arg => arg is T, filter)); + Filters.Add(Guard(arg => arg.Type == typeof(T), filter)); } private static string GetFilterClause(string query, DataTablesPropertyInfo column, List parametersForLinqQuery) From 246e6c4443977309ae5ba37b93589e82aaa29000 Mon Sep 17 00:00:00 2001 From: jandic Date: Tue, 22 Oct 2019 11:18:05 +0200 Subject: [PATCH 3/3] Fix for EnumFilter EnumFilter method was crashing when a value to search was not the exact enum value. This prevented any subsequent filters to be processed. --- .../Processing/TypeFilters.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Mvc.JQuery.DataTables.Common/Processing/TypeFilters.cs b/Mvc.JQuery.DataTables.Common/Processing/TypeFilters.cs index ea17f90..0ba2111 100644 --- a/Mvc.JQuery.DataTables.Common/Processing/TypeFilters.cs +++ b/Mvc.JQuery.DataTables.Common/Processing/TypeFilters.cs @@ -251,11 +251,17 @@ public static string StringFilter(string q, string columnname, DataTablesPropert public static string EnumFilter(string q, string columnname, DataTablesPropertyInfo propertyInfo, List parametersForLinqQuery) { - - if (q.StartsWith("^")) q = q.Substring(1); - if (q.EndsWith("$")) q = q.Substring(0, q.Length - 1); - parametersForLinqQuery.Add(ParseValue(q, propertyInfo.Type)); - return columnname + " == @" + (parametersForLinqQuery.Count - 1); + try + { + if (q.StartsWith("^")) q = q.Substring(1); + if (q.EndsWith("$")) q = q.Substring(0, q.Length - 1); + parametersForLinqQuery.Add(ParseValue(q, propertyInfo.Type)); + return columnname + " == @" + (parametersForLinqQuery.Count - 1); + } + catch (Exception) + { + return null; + } } } }