diff --git a/Source/Extensions/Blazorise.DataGrid/Utils/ExpressionCompiler.cs b/Source/Extensions/Blazorise.DataGrid/Utils/ExpressionCompiler.cs index 16e6be1be3..57c8c2a4ec 100644 --- a/Source/Extensions/Blazorise.DataGrid/Utils/ExpressionCompiler.cs +++ b/Source/Extensions/Blazorise.DataGrid/Utils/ExpressionCompiler.cs @@ -63,7 +63,7 @@ public static IQueryable ApplyDataGridSearch( this IQueryable( column.Field, decimal.Parse(column.SearchValue.ToString()) ) ); + data = data.Where( GetWhereLessThanExpression( column.Field, decimal.Parse( column.SearchValue.ToString() ) ) ); if ( column.ValueType == typeof( double ) || column.ValueType == typeof( double? ) ) data = data.Where( GetWhereLessThanExpression( column.Field, double.Parse( column.SearchValue.ToString() ) ) ); @@ -211,6 +211,53 @@ public static IQueryable ApplyDataGridSearch( this IQueryable( column.Field, TimeSpan.Parse( column.SearchValue.ToString() ) ) ); } + break; + case DataGridColumnFilterMethod.Between: + + if ( column.SearchValue is not object[] rangeSearchValues || rangeSearchValues.Length < 2 ) + break; + + var stringSearchValue1 = rangeSearchValues[0]?.ToString(); + var stringSearchValue2 = rangeSearchValues[1]?.ToString(); + + if ( stringSearchValue1 is null || stringSearchValue2 is null ) + break; + + if ( column.ColumnType == DataGridColumnType.Numeric ) + { + if ( column.ValueType == typeof( decimal ) || column.ValueType == typeof( decimal? ) ) + data = data.Where( GetWhereBetweenExpression( column.Field, decimal.Parse( stringSearchValue1 ), decimal.Parse( stringSearchValue2 ) ) ); + + if ( column.ValueType == typeof( double ) || column.ValueType == typeof( double? ) ) + data = data.Where( GetWhereBetweenExpression( column.Field, double.Parse( stringSearchValue1 ), double.Parse( stringSearchValue2 ) ) ); + + if ( column.ValueType == typeof( float ) || column.ValueType == typeof( float? ) ) + data = data.Where( GetWhereBetweenExpression( column.Field, float.Parse( stringSearchValue1 ), float.Parse( stringSearchValue2 ) ) ); + + if ( column.ValueType == typeof( int ) || column.ValueType == typeof( int? ) ) + data = data.Where( GetWhereBetweenExpression( column.Field, int.Parse( stringSearchValue1 ), int.Parse( stringSearchValue2 ) ) ); + + if ( column.ValueType == typeof( short ) || column.ValueType == typeof( short? ) ) + data = data.Where( GetWhereBetweenExpression( column.Field, short.Parse( stringSearchValue1 ), short.Parse( stringSearchValue2 ) ) ); + } + else if ( column.ColumnType == DataGridColumnType.Date ) + { + if ( column.ValueType == typeof( DateTime ) || column.ValueType == typeof( DateTime? ) ) + data = data.Where( GetWhereBetweenExpression( column.Field, DateTime.Parse( stringSearchValue1 ), DateTime.Parse( stringSearchValue2 ) ) ); + + if ( column.ValueType == typeof( DateTimeOffset ) || column.ValueType == typeof( DateTimeOffset? ) ) + data = data.Where( GetWhereBetweenExpression( column.Field, DateTimeOffset.Parse( stringSearchValue1 ), DateTimeOffset.Parse( stringSearchValue2 ) ) ); + + if ( column.ValueType == typeof( DateOnly ) || column.ValueType == typeof( DateOnly? ) ) + data = data.Where( GetWhereBetweenExpression( column.Field, DateOnly.Parse( stringSearchValue1 ), DateOnly.Parse( stringSearchValue2 ) ) ); + + if ( column.ValueType == typeof( TimeOnly ) || column.ValueType == typeof( TimeOnly? ) ) + data = data.Where( GetWhereBetweenExpression( column.Field, TimeOnly.Parse( stringSearchValue1 ), TimeOnly.Parse( stringSearchValue2 ) ) ); + + if ( column.ValueType == typeof( TimeSpan ) || column.ValueType == typeof( TimeSpan? ) ) + data = data.Where( GetWhereBetweenExpression( column.Field, TimeSpan.Parse( stringSearchValue1 ), TimeSpan.Parse( stringSearchValue2 ) ) ); + } + break; } } @@ -237,7 +284,7 @@ public static IQueryable ApplyDataGridSort( this IQueryable foreach ( var sortByColumn in sortByColumns.OrderBy( x => x.SortIndex ) ) { - var valueGetterExpression = CreateValueGetterExpression( sortByColumn.SortField ); + var valueGetterExpression = string.IsNullOrWhiteSpace( sortByColumn.SortField ) ? CreateValueGetterExpression( sortByColumn.Field ) : CreateValueGetterExpression( sortByColumn.SortField ); if ( firstSort ) { @@ -453,6 +500,13 @@ private static Expression NotEqualsWithExpression( Expression propertyExpression return Expression.IsFalse( EqualsWithExpression( propertyExpression, searchValue ) ); } + /// + /// Builds a where expression. Where the source property contains the searchValue. + /// + /// + /// + /// + /// public static Expression> GetWhereContainsExpression( string sourceProperty, string searchValue ) @@ -465,6 +519,13 @@ public static Expression> GetWhereContainsExpression( return Expression.Lambda>( body, sourceParameterExpression ); } + /// + /// Builds a where expression. Where the source property starts with the searchValue. + /// + /// + /// + /// + /// public static Expression> GetWhereStartsWithExpression( string sourceProperty, string searchValue ) @@ -477,6 +538,13 @@ public static Expression> GetWhereStartsWithExpression( return Expression.Lambda>( body, sourceParameterExpression ); } + /// + /// Builds a where expression. Where the source property ends with the searchValue. + /// + /// + /// + /// + /// public static Expression> GetWhereEndsWithExpression( string sourceProperty, string searchValue ) @@ -489,6 +557,13 @@ public static Expression> GetWhereEndsWithExpression( return Expression.Lambda>( body, sourceParameterExpression ); } + /// + /// Builds a where expression. Where the source property equals the searchValue. + /// + /// + /// + /// + /// public static Expression> GetWhereEqualsExpression( string sourceProperty, string searchValue ) @@ -501,18 +576,33 @@ public static Expression> GetWhereEqualsExpression( return Expression.Lambda>( body, sourceParameterExpression ); } + /// + /// Builds a where expression. Where the source property is not equal to the searchValue. + /// + /// + /// + /// + /// public static Expression> GetWhereNotEqualsExpression( string sourceProperty, string searchValue ) { var sourceParameterExpression = GetParameterExpression(); var sourcePropertyExpression = GetPropertyOrFieldExpression( sourceParameterExpression, sourceProperty ); - + Expression convertSourcePropertyExpression = ConvertToStringExpression( sourcePropertyExpression ); Expression body = NotEqualsWithExpression( convertSourcePropertyExpression, searchValue ); return Expression.Lambda>( body, sourceParameterExpression ); } + /// + /// Builds a where expression. Where the source property is less than the searchValue. + /// + /// + /// + /// + /// + /// public static Expression> GetWhereLessThanExpression( string sourceProperty, TValue searchValue ) @@ -525,6 +615,14 @@ public static Expression> GetWhereLessThanExpression>( Expression.LessThan( convertSourcePropertyExpression, Expression.Constant( searchValue ) ), sourceParameterExpression ); } + /// + /// Builds a where expression. Where the source property is less than or equal to the searchValue. + /// + /// + /// + /// + /// + /// public static Expression> GetWhereLessThanOrEqualExpression( string sourceProperty, TValue searchValue ) @@ -536,6 +634,15 @@ public static Expression> GetWhereLessThanOrEqualExpression>( Expression.LessThanOrEqual( convertSourcePropertyExpression, Expression.Constant( searchValue ) ), sourceParameterExpression ); } + + /// + /// Builds a where expression. Where the source property greater than the searchValue. + /// + /// + /// + /// + /// + /// public static Expression> GetWhereGreaterThanExpression( string sourceProperty, TValue searchValue ) @@ -548,6 +655,14 @@ public static Expression> GetWhereGreaterThanExpression>( Expression.GreaterThan( convertSourcePropertyExpression, Expression.Constant( searchValue ) ), sourceParameterExpression ); } + /// + /// Builds a where expression. Where the source property greater than or equal to the searchValue. + /// + /// + /// + /// + /// + /// public static Expression> GetWhereGreaterThanOrEqualExpression( string sourceProperty, TValue searchValue ) @@ -560,6 +675,32 @@ public static Expression> GetWhereGreaterThanOrEqualExpression return Expression.Lambda>( Expression.GreaterThanOrEqual( convertSourcePropertyExpression, Expression.Constant( searchValue ) ), sourceParameterExpression ); } + /// + /// Builds a where expression. Where the source property is between two provided searchValues. + /// + /// + /// + /// + /// + /// + /// + public static Expression> GetWhereBetweenExpression( + string sourceProperty, + TValue searchValue1, + TValue searchValue2 ) + { + var sourceParameterExpression = GetParameterExpression(); + var sourcePropertyExpression = GetPropertyOrFieldExpression( sourceParameterExpression, sourceProperty ); + + //Note : Another option, would be to not convert and assume the searchValue is of the same type as the source property? + var convertSourcePropertyExpression = Expression.Convert( sourcePropertyExpression, typeof( TValue ) ); + + var value1GreaterThanOrEqualExpression = Expression.GreaterThanOrEqual( convertSourcePropertyExpression, Expression.Constant( searchValue1 ) ); + var value2LessThanOrEqualExpression = Expression.LessThanOrEqual( convertSourcePropertyExpression, Expression.Constant( searchValue2 ) ); + + return Expression.Lambda>( Expression.MakeBinary( ExpressionType.AndAlso, value1GreaterThanOrEqualExpression, value2LessThanOrEqualExpression ), sourceParameterExpression ); + } + /// /// Checks if requested type can bu nullable.