diff --git a/Sieve/Models/SieveOptions.cs b/Sieve/Models/SieveOptions.cs index af0ee0b..4c19575 100644 --- a/Sieve/Models/SieveOptions.cs +++ b/Sieve/Models/SieveOptions.cs @@ -1,4 +1,4 @@ -namespace Sieve.Models +namespace Sieve.Models { public class SieveOptions { @@ -13,5 +13,7 @@ public class SieveOptions public bool IgnoreNullsOnNotEqual { get; set; } = true; public bool DisableNullableTypeExpressionForSorting { get; set; } = false; + + public string CultureNameOfTypeConversion { get; set; } = "en"; } } diff --git a/Sieve/Services/SieveProcessor.cs b/Sieve/Services/SieveProcessor.cs index 5b36ea1..b586b51 100644 --- a/Sieve/Services/SieveProcessor.cs +++ b/Sieve/Services/SieveProcessor.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Globalization; using System.Linq; using System.Linq.Expressions; using System.Reflection; @@ -172,6 +173,8 @@ protected virtual IQueryable ApplyFiltering(TSieveModel model, return result; } + var cultureInfoToUseForTypeConversion = new CultureInfo(Options.Value.CultureNameOfTypeConversion ?? "en"); + Expression outerExpression = null; var parameter = Expression.Parameter(typeof(TEntity), "e"); foreach (var filterTerm in model.GetFiltersParsed()) @@ -198,7 +201,7 @@ protected virtual IQueryable ApplyFiltering(TSieveModel model, var filterValue = isFilterTermValueNull ? Expression.Constant(null, property.PropertyType) - : ConvertStringValueToConstantExpression(filterTermValue, property, converter); + : ConvertStringValueToConstantExpression(filterTermValue, property, converter, cultureInfoToUseForTypeConversion); if (filterTerm.OperatorIsCaseInsensitive && !isFilterTermValueNull) { @@ -309,15 +312,14 @@ private static Expression GenerateFilterNullCheckExpression(Expression propertyV Expression.NotEqual(propertyValue, Expression.Default(propertyValue.Type))); } - private static Expression ConvertStringValueToConstantExpression(string value, PropertyInfo property, - TypeConverter converter) + private static Expression ConvertStringValueToConstantExpression(string value, PropertyInfo property, TypeConverter converter, CultureInfo cultureInfo) { // to allow user to distinguish between prop==null (as null) and prop==\null (as "null"-string) value = value.Equals(EscapeChar + NullFilterValue, StringComparison.InvariantCultureIgnoreCase) ? value.TrimStart(EscapeChar) : value; dynamic constantVal = converter.CanConvertFrom(typeof(string)) - ? converter.ConvertFrom(value) + ? converter.ConvertFrom(null, cultureInfo, value) : Convert.ChangeType(value, property.PropertyType); return GetClosureOverConstant(constantVal, property.PropertyType);